Environment Variables¶
export
unset
Tips and Traps¶
explainshell.com is a great place for learning shell.
Bash-it/bash-it is a great community driven Bash framework.
It is suggested that you avoid writing complicated Bash scripts. IPython is a much better alternative.
Do NOT use
;
to delimit paths passed to a shell command because;
terminates shell commands. Use,
to delimit paths if needed.$(some_command)
is preferred oversome_command
. For more details, please refer to http://mywiki.wooledge.org/BashFAQ/082.
Redirect Output¶
Redirect
stdout
to the fileoutput
andstderr
to another fileerror
.command > output 2> error
Redirect
stderr
tostdout
(&1
), and then redirectstdout
to the fileoutput
.command > output 2>&1
Redirect both
stdout
andstderr
to the fileoutput
.command &> output
It seems to me that
tee
does not work well sometimes. Basically, I have a Python script calling shell comamnds usingsubprocess.call
. The output of the shell commands cannot be captured bytee
. However, redirection using>
partially works. Output ofgit
commands (invoked bysubprocess.call
) cannot be redirected correctly. Actually, it turned out to be that both>
andtee
only works on a file in the current directly. And even so, some output of thegit
command invoked bysubprocess.call
is not captured. I have no idea why that happens. Not sure whether this is a bug in Mac, the Bash shell or due to the fact that I mixed shell commands and the print function in Python.If there are logging from difference resources (e.g., both shell comamnd via
subprocess.call
and the print function in Python) to the standard output, make sure you flush the console so that output/error redirection works as expected.
References¶
http://www.legendu.net/misc/blog/bash-loops/
http://www.legendu.net/misc/blog/bash-functions/
http://www.legendu.net/misc/blog/environment-variables-in-shell/
http://www.legendu.net/misc/blog/autocomplete-for-bash-in-mac/
https://askubuntu.com/questions/625224/how-to-redirect-stderr-to-a-file
https://askubuntu.com/questions/639877/tee-doesnt-get-whole-output-from-the-pipe