Interview Prep/Linux/Packages, Shell & Text Tools

What does `set -euo pipefail` do in a bash script?

IntermediateConceptPackages, Shell & Text Tools
Answer
  • -e: exit on any command returning non-zero (with exceptions inside if, &&, ||).
  • -u: treat unset variables as errors.
  • -o pipefail: a pipeline fails if any command in it fails, not just the last.
#!/usr/bin/env bash
set -euo pipefail
trap 'echo "failed at line $LINENO" >&2' ERR

Without pipefail, curl bad-url | tar xz reports success when curl fails, if tar exits 0.

All Linux questions Roadmap: Linux

More on Packages, Shell & Text Tools