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
stdoutto the fileoutputandstderrto another fileerror.command > output 2> errorRedirect
stderrtostdout(&1), and then redirectstdoutto the fileoutput.command > output 2>&1Redirect both
stdoutandstderrto the fileoutput.command &> outputIt seems to me that
teedoes 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 ofgitcommands (invoked bysubprocess.call) cannot be redirected correctly. Actually, it turned out to be that both>andteeonly works on a file in the current directly. And even so, some output of thegitcommand invoked bysubprocess.callis 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.calland 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