Stop your bash scripts from failing silently when pipes break
Shell Scripting / Bash Tricks
Stop your bash scripts from failing silently when pipes break
🧩 The Challenge
You ever write a pipeline like cat file | grep something | awk ‘{print $1}’ and realize an hour later that the whole thing failed halfway through because grep exited with an error? It’s infuriating because bash just keeps chugging along like everything is fine.
💡 The Fix
Flip the pipefail option on at the top of your script so bash actually pays attention to the exit codes of every single command in the chain. It’s the easiest way to prevent silent data corruption.
set -o pipefail
⚙️ Why It Works
Setting this makes the entire pipeline return the exit status of the last command that actually failed, rather than just returning whatever the very last process in the list spat out. Without it, you’re flying blind every time you chain more than one tool together.
🚀 Pro-Tip: Always combine this with set -e to make your scripts die instantly when any command hits a snag.
Linux Tips & Tricks | © ngelinux.com | 8/17/2026
