$_ CLI In Depth
Learning progress 0%
lesson robustness traps debugging

set -e, -u & -o pipefail

4 min read Module 15: Robust Scripts, Traps & Debugging Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:15

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.

BASH3 lines
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.

BASH3 lines
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.

Warning

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.

BASH3 lines
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.

BASH5 lines
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.

BASH3 lines
#!/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.

BASH1 lines
bash -c 'set -eo pipefail; false | cat; echo "reached end"'; echo "status=$?"
BASH1 lines
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.

set -e exceptions and trap cleanup

Visualizer

set -e stops a script when a command fails, but it skips a few contexts. A trap runs cleanup on exit, and the order of EXIT, ERR and INT decides what runs when.

1 / 6
contextset -e effect
plain failing commandexits the shell
if or while conditiondoes not exit
left side of && or ||does not exit; the command after the last operator still does
command negated with !does not exit
function body, when f runs in such a contextdoes not exit

set -e ends the shell when a command fails. The rows below are the exceptions; the first row is the default rule.

bash -c 'set -e; if false; then echo no; fi; echo "after if"; false || echo "after ||"; ! false; echo "after !"; f() { false; echo "inside f"; }; f || echo "f failed"; false; echo "never printed"'
Terminal lab · 2 steps · real Linux in your browser

Make scripts stop on errors

Write two scripts, one that stops on a failed pipeline and one that stops on an unset variable.

01
Stop on a failed pipeline
Create strict.sh in /workspace. Turn on errexit and pipefail with set -eo pipefail, run a pipeline whose first stage fails, and print reached end after it: ``bash cat > strict.sh <<'EOF' #!/bin/bash set -eo pipefail false | cat echo "reached end" EOF `` Then run bash strict.sh and check that the script stops before the last line.
Show hint for step 1
Without set -e the pipeline failure is reported but the script keeps going. Both options are needed to stop before reached end.
02
Stop on an unset variable
Create unset.sh in /workspace. Turn on nounset with set -u, echo the value of an unset variable named NOPE, and then echo after: ``bash cat > unset.sh <<'EOF' #!/bin/bash set -u echo "$NOPE" echo after EOF `` Run it and read the error message that bash prints.
Show hint for step 2
The error names the variable and ends with unbound variable. The line with after must not run.
Check yourself Question 1 of 4
Concept Check Stopping on the first failure recall

Which set option makes bash exit when a simple command returns a non-zero status?

Recap & Key Takeaways

set -e stops a script when a simple command returns a non-zero status, which prevents later steps from running after a failure.
set -u makes an expansion of an unset variable an error, which catches typos and empty values before they reach dangerous commands.
set -o pipefail makes a pipeline fail when any stage fails, not just its last command.
Most robust scripts start with set -euo pipefail and add explicit handling wherever a failure is acceptable.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue