Just Bash Things

Every now and again I come across a few things that are really useful, or that seem rather odd/unintuitive when working with bash commands/scripts. A couple popped up recently, so I thought I would start a post which will probably expand over time with them, and maybe some ways to help use or work around them.

sudo uses a different path

$ which python 
/home/chris/.pyenv/shims/python
$ sudo which python
/usr/bin/python

bash scripts will continue regardless of errors unless you tell them not too

If any later commands rely on the earlier commands executing successfully (which is often the case), you probably want to set a few sensible defaults at the start of the script

set -e
set -u
set -o pipefail

Check out the more detailed explanation in this awesome article by Kev

Remember you can change the shebang to choose the interpreter

The shebang line is the first line in a script, and chooses the interpreter of the rest of the script

Using your system python3

#!/usr/bin/python3

Using a custom python3 installation (maybe with packages you need for this script)

#!/my/path/to/bin/python3

Or search in the default PATH for the interpreter

#!/usr/bin/env python3