← Back to Bash Course | Chapter 14: Best Practices & Real-World Scripting | Lesson 2 of 7

set -euo pipefail as a Script Header

Adding one line near the top of a script turns on several safety checks at once, making Bash stop and complain the moment something looks wrong instead of pressing on silently.

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

bash
#!/bin/bash
set -euo pipefail

name="demo script"
echo "running $name safely"

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

bash
#!/bin/bash
set -o pipefail

if false | true; then
    echo "unreachable with pipefail active"
else
    echo "pipefail correctly noticed the earlier failure"
fi

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

bash
#!/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"

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

bash
#!/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"
Common Mistakes
  1. Adding set -euo pipefail to 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.
  2. Forgetting that set -e's exemptions (inside if/while conditions, non-last pipeline stages without pipefail) still apply even with the combined flags.
  3. Not testing scripts under set -euo pipefail incrementally, making it hard to tell which of the three flags caused a given failure.
Chapter Summary
  • set -euo pipefail combines 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.