Skip to content

Exit codes and errors

8 min

set -e and the six places it does not fire

A good default, and not a safety net.

set -e ends the script at the first command that fails without being checked. It is worth having. It is also full of holes, and knowing where they are is the difference between using it and relying on it.

It does not fire on a command whose status is being tested: the left of && or ||, the condition of an if, while or until, or a pipeline preceded by !.

Try this

It also does not see inside a pipeline: only the last stage's status counts, so a failing producer feeding a successful consumer looks like success.

set -o pipefail fixes that one, and set -u turns an unset variable into an error rather than an empty string. Together they are the reason set -euo pipefail is the usual first line.

Try this

set -euo pipefail is a good default. It is not a safety net: a script still has to say what it means.

$

Files

The files in the sandbox, with their modes, link counts and sizes
ModeLinksSizeName
-rw-r--r--194budget.md
-rw-r--r--162summary.txt