Stop your nested subshells from swallowing your exit codes
Shell Scripting / Bash Tricks
Stop your nested subshells from swallowing your exit codes
🧩 The Challenge
You ever write a pipeline like if [ $(grep “error” logfile | wc -l) -gt 0 ]; then, only to realize your script kept running even when the file wasn’t there? It drives me nuts that Bash just silently ignores the pipe failure and gives you a zero status code instead.
💡 The Fix
Use the pipefail option in your scripts to force the shell to capture the exit status of every command in a pipeline, not just the last one. It is the only way to make sure a chain of commands actually reports when something dies in the middle.
set -o pipefail
⚙️ Why It Works
Setting this flag updates the built-in $? variable to be the value of the last command in the pipeline that returned a non-zero exit code. Without it, you are effectively flying blind whenever you use pipes.
🚀 Pro-Tip: Always include set -euo pipefail at the top of your scripts to save yourself a week of debugging.
Linux Tips & Tricks | © ngelinux.com | 8/4/2026
