set -e (also written set -o errexit) is a shell option that causes a script to exit immediately when any command returns a non-zero exit code. Without it, a shell script continues executing past errors, which can turn a small bug into a disaster—imagine a backup script that carries on after a failed remove and overwrites data.
#!/usr/bin/env bash
set -euo pipefail
# e: exit on error
# u: error on undefined variable
# o pipefail: pipelines fail if any stage fails
The set -euo pipefail prologue is sometimes called "unofficial bash strict mode". It catches a huge class of bugs: typos in variable names, commands that fail silently, and intermediate pipe failures that would otherwise be masked.
set -e has subtleties: it is disabled inside commands whose exit code is being checked (if, while, until, &&, ||, !), and it does not fire for functions called from if. For more predictable error handling, explicit || return or || exit checks are sometimes preferable. The trap 'cmd' ERR mechanism runs a cleanup command when any error fires, complementing set -e with a log or rollback.
Related terms: Shell Script, Exit Code, pipefail
Discussed in:
- Chapter 14: Shell Scripting — Error Handling
Also defined in: Textbook of Linux