Skip to content
Latchkey

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

Terminal
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 -e

Options

OptionWhat it does
-e / errexitExit on any uncaught non-zero command
-u / nounsetError on unset variable references
-x / xtracePrint commands as they run
+eTurn errexit back off
-o pipefailA 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.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →