/blog/one-bash-idiom

The single Bash idiom I use the most

2026-04-02 · 4 min · #unix #shell
hero diagram · 2:1

It is not a one-liner. It is a habit.

set -euo pipefail

I put this at the top of every shell script I write. It’s three options, and each one saves me from a different class of bug.

set -e — exit on error

Without this, a failing command is silently ignored and the script continues. With this, any non-zero exit code immediately stops the script.

# without -e: cp silently fails, rm -rf runs anyway
cp important-file.txt /nonexistent/path/
rm -rf ./important-file.txt

# with -e: cp fails, script stops, rm never runs

set -u — error on unbound variables

Without this, $UNDEFINED_VAR expands to empty string. With this, it’s a fatal error.

# without -u: silently removes root of filesystem
TARGET_DIR=
rm -rf $TARGET_DIR/

# with -u: fatal error — TARGET_DIR: unbound variable

set -o pipefail — pipeline errors propagate

Without this, cmd1 | cmd2 only checks the exit code of cmd2. With pipefail, if any command in the pipeline fails, the whole pipeline fails.

# without pipefail: script continues even though grep failed
grep "pattern" missing-file.txt | wc -l

# with pipefail: script exits because grep returned 1

Why it matters

I have seen each of these options prevent a production incident at least once.

set -e is the most common. Half the shell scripts I read in the wild have no error handling at all. The script runs to completion, silently fails halfway through, and leaves the system in an inconsistent state.

Add set -euo pipefail to every script. It’s three characters that make you a better operator.