set -e: Usage, Behavior & Common CI Gotchas
set -e (errexit) aborts the script as soon as a command returns non-zero.
set -e is the default safety net for CI scripts - fail fast instead of marching past a broken step. But its exceptions are subtle, and trusting it blindly hides failures.
What it does
set -e (errexit) tells the shell to exit immediately if a simple command exits non-zero. It is the single most common hardening line in CI scripts, usually written set -euo pipefail at the top.
Common usage
set -e # exit on first error
set -euo pipefail # errexit + nounset + pipefail
set -x # also trace each command
# disable temporarily around a command allowed to fail:
set +e; maybe_fails; rc=$?; set -eOptions
| Option | What it does |
|---|---|
| -e / errexit | Exit on any uncaught non-zero command |
| -u / nounset | Error on unset variable references |
| -x / xtrace | Print commands as they run |
| +e | Turn errexit back off |
| -o pipefail | A pipeline fails if any stage fails |
Common errors in CI
set -e has exceptions that silently swallow failures: a command in an if/while condition, on the left of && or ||, or in a pipeline (only the last stage is checked without pipefail) does NOT trigger exit. So cmd_that_fails | tee log keeps going, and foo() { false; echo still-here; } runs on. Also, set -e inside a function or command substitution behaves differently. Pair it with -o pipefail and check exit codes explicitly for commands you actually care about.