Bird
0
0

Which of the following is the correct way to write a bash script that exits on error?

easy📝 Syntax Q3 of 15
Bash Scripting - Error Handling
Which of the following is the correct way to write a bash script that exits on error?
A#!/bin/bash set e command1 command2
B#!/bin/bash set +e command1 command2
C#!/bin/bash exit -e command1 command2
D#!/bin/bash set -e command1 command2
Step-by-Step Solution
Solution:
  1. Step 1: Check the correct syntax for enabling exit on error

    The correct syntax is set -e after the shebang line.
  2. Step 2: Validate the script structure

    #!/bin/bash set -e command1 command2 uses set -e correctly; others use invalid or disabling commands.
  3. Final Answer:

    #!/bin/bash\nset -e\ncommand1\ncommand2 -> Option D
  4. Quick Check:

    Correct syntax includes set -e [OK]
Quick Trick: Place set -e right after #!/bin/bash [OK]
Common Mistakes:
MISTAKES
  • Using set +e instead of set -e
  • Writing exit -e which is invalid
  • Missing the dash in set -e

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes