Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash strict mode: set -euo pipefail

Reference for Bash strict mode: what set -e, -u and -o pipefail do, the situations where set -e silently does nothing, and safer patterns for error handling.

"Strict mode" is the nickname for putting set -euo pipefail near the top of a script. Each option removes one way a script can keep going after something has gone wrong: -e stops on a failing command, -u stops on an unset variable, and -o pipefail makes a pipeline fail if any stage failed.

The combination catches many bugs early, but it is not a safety net. set -e in particular has several exceptions that surprise even experienced people, so treat strict mode as a helpful default, and still check the commands that matter explicitly.

Terminal
#!/usr/bin/env bash
set -euo pipefail

Try it

Bash What each option changes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Where set -e does not trigger

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
set -e          # errexit: exit when a simple command fails
set -u          # nounset: expanding an unset variable is an error
set -o pipefail # a pipeline fails if any stage fails
set -euo pipefail
set +e          # turn an option off again (also +u, +o pipefail)
Name Description
-e / -o errexit Exit immediately when a command returns non-zero, except in the situations listed below.
-u / -o nounset Treat an unset variable or parameter (other than @ and *) as an error. The shell exits in a script.
-o pipefail The pipeline status is the last non-zero status of any stage, or 0 if all succeeded.
-x Not part of the trio, but often added while debugging: print each command.

Returns: set itself returns 0. A script aborted by -e exits with the status of the failing command; -u aborts with a non-zero status (the exact number differs between Bash versions).

Errors and edge cases: set -e is ignored for commands that are part of an if/while/until condition, the non-final parts of && and || lists, commands negated with !, and (important) any command inside a function or subshell while that function is called in such a context.

Usage notes

  • set -e is not a try/catch. Behavior depends on where a command runs, which is why many style guides prefer explicit checks (cmd || exit 1) or an ERR trap that reports the line: trap 'echo "failed at line $LINENO" >&2' ERR.
  • -u and arrays. In Bash before 4.4, expanding an empty array "${arr[@]}" under -u was an error. Bash 4.4+ accepts it.
  • -u and optional input. Use ${1:-} for an optional positional parameter and ${VAR:-default} for environment variables that may be absent.
  • grep returns 1 when nothing matches. Under -e and pipefail, grep pattern file | wc -l can kill the script. Write grep ... || true where "no match" is normal.
  • -e in sourced files and $( ). A subshell inherits -e in Bash only in POSIX mode or with shopt -s inherit_errexit; without that option, x=$(false; echo hi) continues.
  • Cleanup. Combine strict mode with trap cleanup EXIT so temporary files are removed even when the script aborts.
  • Portability. pipefail exists in Bash, Zsh, ksh93 and BusyBox, but not in plain POSIX sh before the 2024 standard; dash 0.5.12 and newer has it.