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.
#!/usr/bin/env bash set -euo pipefail
Try it
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.
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
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 -eis not a try/catch. Behavior depends on where a command runs, which is why many style guides prefer explicit checks (cmd || exit 1) or anERRtrap that reports the line:trap 'echo "failed at line $LINENO" >&2' ERR.-uand arrays. In Bash before 4.4, expanding an empty array"${arr[@]}"under-uwas an error. Bash 4.4+ accepts it.-uand optional input. Use${1:-}for an optional positional parameter and${VAR:-default}for environment variables that may be absent.grepreturns 1 when nothing matches. Under-eandpipefail,grep pattern file | wc -lcan kill the script. Writegrep ... || truewhere "no match" is normal.-ein sourced files and$( ). A subshell inherits-ein Bash only in POSIX mode or withshopt -s inherit_errexit; without that option,x=$(false; echo hi)continues.- Cleanup. Combine strict mode with
trap cleanup EXITso temporary files are removed even when the script aborts. - Portability.
pipefailexists in Bash, Zsh, ksh93 and BusyBox, but not in plain POSIXshbefore the 2024 standard; dash 0.5.12 and newer has it.