Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Hot answers tagged

41

In bash, you can compare $BASHPID to $$ $ ( if [ "$$" -eq "$BASHPID" ]; then echo not subshell; else echo subshell; fi ) subshell $ if [ "$$" -eq "$BASHPID" ]; then echo not subshell; else echo subshell; fi not subshell If you're not in bash, $$ should remain the same in a subshell, so you'd need some other way of getting your actual process ID. One way ...


38

How about BASH_SUBSHELL? BASH_SUBSHELL       Incremented by one within each subshell or subshell environment when the shell       begins executing in that environment. The initial value is 0. $ echo $BASH_SUBSHELL 0 $ (echo $BASH_SUBSHELL) 1


18

[this should've been a comment, but my comments tend to be deleted by moderators, so this will stay as an answer that I could use it as a reference even if deleted] Using BASH_SUBSHELL is completely unreliable as it be only set to 1 in some subshells, not in all subshells. $ (echo $BASH_SUBSHELL) 1 $ echo $BASH_SUBSHELL | cat 0 Before claiming that the ...


8

You could call the command once, redirect the output, then remove the output if there were no differences: diff a c > output.txt && rm output.txt


5

What about temporary file? diff a c > /tmp/output.txt if [ $? != 0 ]; then mv /tmp/output.txt /my/folder/output.txt; else rm -f /tmp/output.txt; fi replace the -f with -i if you want delete confirmation dialog. This way you only run command twice, no temporary variables and no 'middleman' be it echo, printf or anything else.


4

diff is a relatively expensive command, at least if the files are different. Calculating a minimal set of changes is (relatively) CPU intensive. So its understandable not to want to do that twice. cmp, however, is cheap, CPU-wise. Presuming these files are of a reasonable size (I doubt you'd call diff on multi-GB files), it will have almost no performance ...


3

You can use awk: awk '{printf "%c\t%s\n", NR+96, $0}' (97 being the ASCII decimal value of a) % seq 1 10 | awk '{printf "%c\t%s\n", NR+96, $0}' a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10


2

Short answer; it can't be done, and for good reason! If that were so, than anyone could become root. Think about it. :) HTH


2

You should not export the functions. Instead I suggest to source .bash_aliases for all bash instances that need it. (I guess you need the functions in interactive bash shells only.) A similar problem is mentioned here: /bin/sh: error importing function definition for `some-function', where the accepted answer proposes to rename the functions.


2

The simple solution is to not source them. Since ~/.bash_aliases isn't a standard file, you must be explicitly sourcing it in one of your config files. So an easy solution would be to find the line that source it (most likely in your ~/.bashrc) and change it from this (or whatever your system has, this one is from Ubuntu): if [ -f ~/.bash_aliases ]; then ...


2

gdb -p PID -batch -ex 'p fflush(stdout)' As with any debugging and hacking, YMMV.


2

Do you have access to the source of the running programs? Forcing a flush of an arbitrary executable is, while not theoretically impossible, very difficult. You would need to find the fflush function in the code and the stdout argument, then interrupt the execution of the program, arrange for the call to fflush, then continue execution. If the program is ...


2

You cannot "upgrade" an already running shell. You can however a) create a pty and run another shell in it with script /dev/null b) fiddle with your local terminal so it doesn't intepret the intr, eof, eol and other keys specially, but pass them through. $ nc -lvp 9999 Listening on [0.0.0.0] (family 0, port 9999) [ncat -4 localhost 9999 -e /bin/bash ...


1

That's because brace expansion happens before variable expansion. You can use seq instead: num=5 for i in $(seq 1 $num) ; do echo hello done


1

With bash using paste, head, wc and printf: # generate somefile $ printf 'line %s of file\n' {1..5} > somefile # then paste $ paste -d ' ' <(printf '%s\n' {a..z}) somefile a line 1 of file b line 2 of file c line 3 of file d line 4 of file e line 5 of file f g ... As we don't know how many characters we need to generate (a-e in this example), we ...


1

It's your use of double quotes. You cannot use double quotes "inside" double quotes unless they are escaped. Instead you could either escape your double quotes or more preferrably alternate quotes by using single quotes instead: #!/usr/bin/perl use warnings; use strict; exec("sed -i -r '4{s/\{\+//; s/\+}//; s/\[-.*-]//g}' error.txt");


1

You should quote the variable in your echo command: $ var=? $ echo "$var" ?


1

As @panki said It was simple: $> fn(){DATA=$(sed -n ''"$2"'p' $1 | trans -no-init -no-warn -b -t ru) && \ sed -i ''"$2"'a '"$DATA"'' $1} && \ fn file_for_transalate.md 82


Only top voted, non community-wiki answers of a minimum length are eligible