Bird
Raised Fist0
Bash Scriptingscripting~20 mins

set -o pipefail in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pipefail Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this script with pipefail enabled?
Consider this bash script run with set -o pipefail enabled:

echo "hello" | grep "bye" | wc -l

What is the exit status of the entire pipeline?
Bash Scripting
set -o pipefail

echo "hello" | grep "bye" | wc -l
AExit status is 0
BExit status is 2
CExit status is 1
DExit status is the exit status of wc -l
Attempts:
2 left
💡 Hint
Remember, with pipefail, the pipeline exit status is the rightmost non-zero status or zero if all succeed.
💻 Command Output
intermediate
2:00remaining
What happens without pipefail in this pipeline?
Run this pipeline without set -o pipefail:

echo "hello" | grep "bye" | wc -l

What is the exit status of the pipeline?
Bash Scripting
echo "hello" | grep "bye" | wc -l
AExit status is 0
BExit status is 1
CExit status is 2
DExit status is the exit status of grep
Attempts:
2 left
💡 Hint
Without pipefail, the pipeline exit status is the last command's exit status.
📝 Syntax
advanced
1:30remaining
Which command correctly enables pipefail in bash?
You want to enable pipefail in your bash script. Which command is correct?
Aset -o pipefail
Bset pipefail on
Cenable pipefail
Dset -pipefail
Attempts:
2 left
💡 Hint
The correct syntax uses 'set -o' followed by the option name.
🚀 Application
advanced
2:00remaining
How to detect failure in any command of a pipeline?
You want your bash script to fail if any command in a pipeline fails. Which approach works best?
ACheck exit status of the last command only
BUse 'set -o pipefail' before running the pipeline
CUse 'set -e' only without pipefail
DRun each command separately without a pipeline
Attempts:
2 left
💡 Hint
Pipefail makes the pipeline exit status reflect any failure inside it.
🧠 Conceptual
expert
2:30remaining
Why is 'set -o pipefail' important in automation scripts?
In automation scripts, why is enabling set -o pipefail considered a best practice?
AIt automatically retries failed commands in a pipeline.
BIt speeds up the execution of pipelines by running commands in parallel.
CIt disables error messages from pipeline commands.
DIt ensures the script detects failure in any command within a pipeline, preventing false success.
Attempts:
2 left
💡 Hint
Think about how pipelines report success or failure by default.

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