Bird
0
0

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🚀 Application Q15 of 15
Bash Scripting - Error Handling
You want to write a script that stops immediately if any command in a pipeline fails. Which combination of bash options should you use?
Aset -o pipefail only
Bset -e only
Cset -e and set -o pipefail
Dset -u and set -o pipefail
Step-by-Step Solution
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]
Quick Trick: Use both set -e and pipefail to stop on any pipeline error [OK]
Common Mistakes:
MISTAKES
  • Using only pipefail without set -e
  • Using only set -e without pipefail
  • Confusing set -u (undefined vars) with error exit

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes