Command for Safe Shell Scripts
I came across some good advice form the MIT Student Information Processing Board regarding writting safe shell scripts.
The recommandation is to use the following command in bash scripts1:
set -euf -o pipefail
Exit the whole script if a command fail, instead of resuming on the next line.
Unset variables are treated as an error and script exit immediately.
Disable filename expansion (globbing) upon seeing
? etc. Obviously, remove for globbing.
set -o pipefail
Causes pipeline (commands with
|) to produce failure return code if any command errors, not just the last one. With
set -e set above, this will exit our script.
-oflag for dash.