Bird
Raised Fist0
Bash Scriptingscripting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the set -o pipefail command do in a bash script?
easy
A. It makes the pipeline return the exit status of the last failed command.
B. It disables all error checking in the script.
C. It forces the script to ignore errors in pipelines.
D. It restarts the pipeline if any command fails.

Solution

  1. Step 1: Understand pipeline exit status behavior

    Normally, a pipeline returns the exit status of the last command only, ignoring failures in earlier commands.
  2. Step 2: Effect of set -o pipefail

    This option changes the pipeline's exit status to that of the last failed command, making error detection more reliable.
  3. Final Answer:

    It makes the pipeline return the exit status of the last failed command. -> Option A
  4. Quick Check:

    Pipeline failure detection = It makes the pipeline return the exit status of the last failed command. [OK]
Hint: Remember: pipefail catches failures anywhere in a pipeline [OK]
Common Mistakes:
  • Thinking it disables error checking
  • Assuming it restarts pipelines on failure
  • Believing it ignores errors in commands
2. Which of the following is the correct way to enable pipefail in a bash script?
easy
A. set pipefail on
B. set -o pipefail
C. enable pipefail
D. pipefail enable

Solution

  1. Step 1: Recall bash syntax for options

    Bash uses set -o option_name to enable shell options.
  2. Step 2: Identify correct syntax for pipefail

    The correct command to enable pipefail is set -o pipefail.
  3. Final Answer:

    set -o pipefail -> Option B
  4. Quick Check:

    Enable pipefail = set -o pipefail [OK]
Hint: Use 'set -o' before option name to enable it [OK]
Common Mistakes:
  • Using 'set pipefail on' which is invalid syntax
  • Trying 'enable pipefail' which is not a bash command
  • Writing 'pipefail enable' which is incorrect order
3. Consider this script snippet with set -o pipefail enabled:
set -o pipefail
false | true | true
echo $? 
What will be the output?
medium
A. 1
B. 0
C. 2
D. 3

Solution

  1. Step 1: Understand commands in pipeline

    The first command false returns exit status 1 (failure), the others return 0 (success).
  2. Step 2: Effect of set -o pipefail

    With pipefail, the pipeline exit status is the exit status of the last failed command, which is 1.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    Pipeline exit status = last failure = 1 [OK]
Hint: With pipefail, pipeline returns last error code [OK]
Common Mistakes:
  • Assuming output is 0 because last command succeeds
  • Thinking output is sum of exit codes
  • Confusing exit codes with command count
4. You have this script:
set -o pipefail
cat file.txt | grep 'pattern' | sort
if [ $? -ne 0 ]; then
  echo 'Error detected'
fi
But it never prints 'Error detected' even if grep fails. What is the likely problem?
medium
A. The script needs set -e instead.
B. The if condition syntax is incorrect.
C. The pipeline commands are all succeeding.
D. set -o pipefail was not enabled before the pipeline.

Solution

  1. Step 1: Check when pipefail is enabled

    If set -o pipefail is enabled after the pipeline runs, it won't affect that pipeline's exit status.
  2. Step 2: Confirm error detection depends on pipefail

    Without pipefail enabled before the pipeline, the exit status is from the last command, so failures in earlier commands are missed.
  3. Final Answer:

    set -o pipefail was not enabled before the pipeline. -> Option D
  4. Quick Check:

    Enable pipefail before pipeline = set -o pipefail was not enabled before the pipeline. [OK]
Hint: Enable pipefail before running pipelines to catch errors [OK]
Common Mistakes:
  • Assuming set -e alone catches pipeline errors
  • Thinking if syntax is wrong
  • Believing all commands always succeed
5. You want to write a script that stops immediately if any command in a pipeline fails. Which combination of bash options should you use?
hard
A. set -o pipefail only
B. set -e only
C. set -e and set -o pipefail
D. set -u and set -o pipefail

Solution

  1. Step 1: Understand set -e behavior

    set -e makes the script exit immediately if any command returns a non-zero status.
  2. Step 2: Understand set -o pipefail role

    Without pipefail, set -e ignores failures in earlier pipeline commands because the pipeline exit status is from the last command.
  3. Step 3: Combine both for strict error detection

    Using both set -e and set -o pipefail ensures the script exits on any failure inside pipelines.
  4. Final Answer:

    set -e and set -o pipefail -> Option C
  5. Quick Check:

    Exit on any failure = set -e + pipefail = set -e and set -o pipefail [OK]
Hint: Use both set -e and pipefail to stop on any pipeline error [OK]
Common Mistakes:
  • Using only pipefail without set -e
  • Using only set -e without pipefail
  • Confusing set -u (undefined vars) with error exit
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard