Why the default is dangerous
A bash script reads its commands one by one and runs each one, whatever the previous one returned. The exit status of each command is stored in $?, and nothing checks it unless you write the check yourself. That is convenient at the prompt, where you want to see the next command run no matter what. In a script that deploys or deletes things, it means one failed step can be followed by many steps that assume it worked.
cd /no/such/dir
rm -rf ./build/*
echo "cleaned"If the cd fails, the shell stays in the old directory, and the rm runs there. The script then prints cleaned. The three lines look reasonable, but the second line now acts on the wrong place. Each of the three options in this unit prevents one kind of this failure.
set -e: stop on the first failure
set -e, which is the same as set -o errexit, makes bash exit as soon as a simple command returns a non-zero status. The exit status of the script becomes the status of the command that failed. There are several exceptions, and the next unit covers them, but the basic rule is that a failing command ends the script.
set -e
cd /no/such/dir
echo "never printed"The cd fails, and bash exits with a non-zero status before the echo runs. The message from cd is printed to standard error, which tells you what happened. In an interactive terminal, set -e is dangerous, because it would close your shell on the first mistake, so use it in scripts rather than at the prompt.
Running set -e at an interactive prompt can end your terminal session when any command fails. Put it in a script, or run it inside bash -c when you only want to test it.
set -u: catch unset variables
set -u, or set -o nounset, makes any expansion of an unset variable an error. Without it, a typo such as $DESTIONATION expands to nothing, and a command such as rm -rf "$dir"/* with an empty dir becomes rm -rf /*. With it, bash reports the variable name and exits.
set -u
echo "dir is $dest_dir"
echo "never printed"Bash prints an error that ends with unbound variable and exits. Use the default forms from the parameter expansion unit when a variable may legitimately be unset, such as ${dest_dir:-/tmp}. Bash also allows the common idiom "$@" with no arguments under set -u in recent versions, but check your own edge cases.
set -o pipefail: the status of a whole pipeline
Without pipefail, the status of a pipeline is the status of its last command. A failure early in the pipeline is hidden by a successful final stage, so set -e does not see it. Setting pipefail makes the pipeline report the rightmost non-zero status among its stages, or zero if all stages succeed.
false | cat
echo "plain status=$?"
set -o pipefail
false | cat
echo "pipefail status=$?"The first status is zero, because cat succeeded. After pipefail is turned on, the same pipeline reports 1, because false failed. The PIPESTATUS array keeps the status of every stage, which helps when you need to know which stage failed.
Putting the three together
Most scripts begin with one line that enables all three: set -euo pipefail. It is a strong default, and the scripts in the later units assume it. Think of the line as a contract that says any unexpected failure or unexpected empty value should stop the run. Code that is meant to tolerate a failure must then say so explicitly, for example with || true, or with a test in an if.
#!/usr/bin/env bash
set -euo pipefail
printf "%s\n" "strict mode on"Hands-on: see each option work
Run the two blocks below in order. Each one runs a short script in a fresh bash, so your own shell is not affected. The first shows pipefail, and the second shows nounset.
bash -c 'set -eo pipefail; false | cat; echo "reached end"'; echo "status=$?"bash -c 'set -u; echo "$NOPE"; echo "after"'; echo "status=$?"The first run does not print reached end, and it reports a status of 1. The second run prints an unbound variable error and never prints after. The lab for this unit asks you to write two scripts that stop in exactly these ways, and it checks the result.