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
Everyone has had that moment where they pipe a command into a grep or a head, the first part fails, but the script just keeps on rolling like nothing happened. You end up with a log file full of nonsense because bash didn’t realize the middle of your pipe train crashed.
💡 The Fix
Turn on the pipefail option to force bash to capture the exit status of every command in a pipeline, not just the last one. It stops the script immediately if any part of the chain goes sideways.
set -o pipefail
⚙️ Why It Works
Normally, bash only cares about the exit code of the very last command in a sequence, but this flag makes the pipeline exit with the status of the first command that actually fails. It saves you from processing half-baked data when your upstream process dies quietly.
🚀 Pro-Tip: Always include this right at the top of your scripts alongside set -e to stop bugs before they propagate.
Linux Tips & Tricks | © ngelinux.com | 8/29/2026
