0
0
Bash Scriptingscripting~10 mins

set -o pipefail in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - set -o pipefail
Start Shell
Run Pipeline: cmd1 | cmd2 | cmd3
Each command runs in order
Check exit status of each command
With pipefail ON
Exit status = last non-zero command's status
With pipefail OFF
Exit status = last command's status
End
This flow shows how the shell runs a pipeline and how the exit status is determined differently when pipefail is on or off.
Execution Sample
Bash Scripting
set -o pipefail
false | true | true
echo $?  # prints exit status
Runs a pipeline where the first command fails, then prints the exit status with pipefail enabled.
Execution Table
StepCommand in PipelineCommand Exit StatusPipefail On?Pipeline Exit StatusExplanation
1false1Yes1First command fails with exit status 1
2true0Yes1Second command succeeds, but pipeline exit status stays 1 due to pipefail
3true0Yes1Third command succeeds, pipeline exit status remains 1
4false1No1With pipefail off, pipeline exit status is last command's status (false=1)
5true0No0Pipeline exit status is 0 because last command succeeded
💡 Pipeline exit status depends on pipefail: with it ON, it's the rightmost non-zero status; with it OFF, it's last command's status.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
PIPEFAILoffonononoffoff
PIPELINE_EXIT_STATUSN/A11110
Key Moments - 3 Insights
Why does the pipeline exit status change when set -o pipefail is enabled?
Because with pipefail ON, the shell returns the exit status of the rightmost failing command in the pipeline instead of the last command's status, as shown in execution_table rows 1-3.
What happens if the last command in the pipeline succeeds but an earlier command fails?
With pipefail ON, the pipeline exit status reflects the earlier failure (non-zero), not the last command's success, as seen in execution_table row 3.
Does set -o pipefail affect the commands' execution order or output?
No, it only changes how the shell reports the pipeline's exit status, not the commands' execution or their output.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the pipeline exit status after step 3 with pipefail ON?
A1
B0
C2
DUndefined
💡 Hint
Check the 'Pipeline Exit Status' column in row 3 where pipefail is Yes.
At which step does the pipeline exit status become 0 with pipefail OFF?
AStep 1
BStep 4
CStep 5
DStep 3
💡 Hint
Look at the 'Pipefail On?' column and find where it is No and exit status changes.
If the first command in the pipeline succeeds but the last command fails, what would be the pipeline exit status with pipefail ON?
A0 (success)
BNon-zero (failure)
CDepends on the middle command
DAlways 1
💡 Hint
Remember pipefail returns the rightmost failing command's status, not the last command's.
Concept Snapshot
set -o pipefail

- Enables pipeline failure detection
- Pipeline exit status is rightmost failing command's status
- Without it, exit status is last command's status
- Helps catch errors in any pipeline stage
- Use before running pipelines to improve error handling
Full Transcript
This lesson shows how the bash shell handles the exit status of pipelines differently when set -o pipefail is enabled. Normally, the shell returns the exit status of the last command in a pipeline. But with pipefail on, it returns the exit status of the rightmost command that fails. The example runs a pipeline where the first command fails and the others succeed. With pipefail on, the pipeline exit status is the failure code from the first command. Without pipefail, it is the success code from the last command. This helps scripts detect errors early in pipelines. The execution table traces each command's exit status and the resulting pipeline status. The variable tracker shows how the PIPEFAIL option and pipeline exit status change step by step. Key moments clarify common confusions about how pipefail affects exit status but not command execution. The quiz tests understanding of pipeline exit status behavior with pipefail on and off. The snapshot summarizes the key points for quick reference.