Stop your bash scripts from swallowing exit codes
Shell Scripting / Bash Tricks
Stop your bash scripts from swallowing exit codes
🧩 The Challenge
Ever notice how a script fails silently because you chained commands with pipes and the middle one tanked? You think everything finished clean, but the data is garbage and you’ve already moved on to the next task.
💡 The Fix
Setting a specific shell option makes your script exit the moment any command in a pipeline fails, rather than just returning the exit code of the very last process. It turns a silent disaster into an immediate stop.
set -o pipefail
⚙️ Why It Works
By default, bash only cares about the exit status of the final command in a pipe. This setting forces it to look at the whole sequence and abort if anything anywhere in that line hits a non-zero exit code.
🚀 Pro-Tip: Always pair this with set -e if you want your scripts to actually stop instead of limping through errors.
Linux Tips & Tricks | © ngelinux.com | 10/9/2026
