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

Quick Explanation

set -e

Exit the whole script if a command fail, instead of resuming on the next line.

set -u

Unset variables are treated as an error and script exit immediately.

set -f

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.

  1. Remove -o flag for dash.

    ↩︎