Mastering Process Flow Control with Bash Pipeline Return Status
Shell Scripting / Bash Tricks
Mastering Process Flow Control with Bash Pipeline Return Status
🧩 The Challenge
In complex bash pipes, the exit status of a command is masked by the final command in the chain, causing silent failures of intermediate tasks. You often need to know if an early step in a pipe failed without writing temporary files.
💡 The Fix
Enable the pipefail shell option to force the pipeline to return the exit status of the last command to exit with a non-zero status. This ensures that any error in the sequence properly triggers failure handling in your script.
set -o pipefail
cmd1 | cmd2 | cmd3
if [ $? -ne 0 ]; then echo "A process in the pipeline failed"; fi
⚙️ Why It Works
By default, bash only returns the status of the final command in a pipe, but setting pipefail captures the error code from any command within the pipeline that returns a non-zero value.
🚀 Pro-Tip: Use the PIPESTATUS array variable to inspect the specific exit codes of every individual command executed in the most recent foreground pipeline.
Linux Tips & Tricks | © ngelinux.com | 7/8/2026
