set -euo pipefail as a Script Header
In this page:
The Combined Header
set -euo pipefail is a compact way to enable three protections at once: exit immediately on an unhandled command failure, error on referencing an unset variable, and make a pipeline's exit status reflect any failing stage, not just the last one.
Example: The Combined Header
#!/bin/bash
set -euo pipefail
name="demo script"
echo "running $name safely"
Login to try C/C++/Java/PHP code in the editor
Seeing pipefail's Effect
Without pipefail, a failing first stage in a pipeline is masked by a successful later stage. With it enabled, the pipeline's exit status becomes the first non-zero status among all of its stages.
Example: Seeing pipefail's Effect
#!/bin/bash
set -o pipefail
if false | true; then
echo "unreachable with pipefail active"
else
echo "pipefail correctly noticed the earlier failure"
fi
Login to try C/C++/Java/PHP code in the editor
Testing Strict Mode Incrementally
When retrofitting set -euo pipefail onto an existing script, it helps to enable the flags one at a time (or test in a subshell) so any newly surfaced failure can be traced to a specific flag rather than all three at once.
Example: Testing Strict Mode Incrementally
#!/bin/bash
(
set -u
echo "testing just -u in isolation"
)
(
set -e
echo "testing just -e in isolation"
)
echo "both isolated tests completed without crashing the main script"
Login to try C/C++/Java/PHP code in the editor
A Realistic Script Header
In practice, set -euo pipefail is placed right after the shebang line, before any other logic, so the entire script benefits from these protections from the very first command.
Example: A Realistic Script Header
#!/bin/bash
set -euo pipefail
process_item() {
local item=$1
echo "processing: $item"
}
for item in alpha beta gamma; do
process_item "$item"
done
echo "all items processed successfully"
Login to try C/C++/Java/PHP code in the editor
- Adding
set -euo pipefailto an existing large script and being surprised when it suddenly "breaks", when really it's exposing bugs (unset variables, ignored failures) that were already there. - Forgetting that
set -e's exemptions (insideif/whileconditions, non-last pipeline stages withoutpipefail) still apply even with the combined flags. - Not testing scripts under
set -euo pipefailincrementally, making it hard to tell which of the three flags caused a given failure.
set -euo pipefailcombines exit-on-error (-e), error-on-unset-variable (-u), and pipeline-failure-propagation (-o pipefail) into one line.- It's a widely recommended defensive header for non-trivial bash scripts, though it doesn't catch every possible bug.
- Adding it to a pre-existing script often surfaces previously silent bugs rather than introducing new ones.
- It's still worth explicit error handling for the specific failures you most care about, rather than relying on strict mode alone.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- Quoting Pitfalls and Why to Always Quote Variables
- set -euo pipefail as a Script Header
- Structuring a Script (Functions, main() Pattern, Exit at the End)
- Idempotent Scripts (Safe to Re-Run)
- Logging in Scripts (Timestamped log Function)
- Writing Portable Scripts (Bash-isms vs POSIX sh)
- Capstone: A Real-World Backup/Cleanup Script