Shell Scripting / Bash Tricks
Stop your scripts from crashing when a pipe breaks
🧩 The Challenge
You know that feeling when you pipe a long log stream into grep, only for your script to suddenly barf a broken pipe error and die because the downstream process quit early? It’s infuriating because your master process just craters for no good reason.
💡 The Fix
Setting the pipefail option allows the shell to see the actual exit status of the piped commands rather than just the last one, and ignoring SIGPIPE keeps your scripts from panicking.
set -o pipefail
trap '' PIPE
⚙️ Why It Works
Adding these lines at the top of your script forces Bash to care about every link in the chain and tells the shell to stop sending termination signals when a downstream reader disconnects.
🚀 Pro-Tip: Use this in every cron job wrapper you write to stop silent failures from turning into production midnight wake-up calls.
Linux Tips & Tricks | © ngelinux.com | 10/20/2026
