set -euo pipefail Reference for CI Scripts
set -euo pipefail is the strict-mode preamble that makes a bash script abort on the first real error.
Strict mode is the single most valuable line in a CI script. It turns silent failures into loud ones, but each flag has edge cases worth knowing before you rely on it.
Common flags/usage
- -e / errexit: exit immediately when a command returns non-zero
- -u / nounset: error when an unset variable is referenced
- -o pipefail: a pipeline fails if any stage fails, not just the last
- -x / xtrace: print each command before running it (debugging)
- set +e: temporarily disable errexit around a command you handle
Example
shell
#!/usr/bin/env bash
set -euo pipefail
: "${DEPLOY_ENV:?DEPLOY_ENV must be set}"
curl -fsS "https://api/${DEPLOY_ENV}/health" | jq -e '.ok'In CI
set -e does NOT fire for a command in an if/while condition, on the left of && or ||, or in a function whose result is tested, so a failing command there is swallowed. pipefail is what makes curl ... | jq fail when curl fails. With -u, reference optional vars as "${VAR:-}" to avoid an unbound-variable abort.
Key takeaways
- set -euo pipefail aborts on the first error, unset variable, or failed pipe stage.
- -e is suppressed in conditionals and before && / ||, so failures there are swallowed.
- Use "${VAR:-}" for optional variables so -u does not abort on them.
Related guides
trap Command Reference for CI Scriptstrap runs cleanup handlers on signals and exit in CI scripts. Reference for EXIT, ERR, and signal traps so te…
grep Command Reference for CI Scriptsgrep searches text for matching lines in CI. Reference for -r, -E, -i, -q, and -v, plus the exit-code-1-means…
head Command Reference for CI Scriptshead prints the first lines or bytes of input in CI. Reference for -n, -c, and negative counts, plus the SIGP…
timeout Command Reference for CI Scriptstimeout caps how long a command may run in CI, preventing hangs. Reference for duration units, -k kill-after,…