Skip to content
Latchkey

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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →