set -euo pipefail & trap in Bash
From the Bash Scripting cheat sheet · Exit Codes & Error Handling · verified Jul 2026
set -euo pipefail & trap
Strict mode and cleanup handlers.
bash
# Quick Reference
set -euo pipefail # strict mode (put near the top)
trap 'echo "cleanup"' EXIT
trap 'echo "error on line $LINENO"' ERR💡 Start scripts with set -euo pipefail so failures stop the script instead of cascading.
⚡ trap ... EXIT guarantees cleanup (temp files, locks) runs on every exit path.
📌 With -u, reference optional vars as ${VAR:-} to avoid an unbound-variable error.
🟢 Send error messages to stderr with >&2 so they are not captured by $(...).
error-handlingsettrap