Bird
0
0

You have this script:

medium📝 Debug Q14 of 15
Bash Scripting - Error Handling
You have this script:
#!/bin/bash
set -e
echo "Start"
ls /nonexistent_directory
echo "End"
The script exits immediately after the ls command. How can you fix it to continue even if ls fails?
ARemove <code>set -e</code> from the script.
BAdd <code>|| true</code> after the <code>ls</code> command.
CChange <code>ls</code> to <code>ls -e</code>.
DAdd <code>set +e</code> at the start of the script.
Step-by-Step Solution
Solution:
  1. Step 1: Understand why script exits

    set -e causes the script to exit on any command failure, so ls /nonexistent_directory failing stops the script.
  2. Step 2: Use || true to ignore error

    Appending || true makes the command always succeed, preventing exit.
  3. Final Answer:

    Add || true after the ls command. -> Option B
  4. Quick Check:

    Use command || true to bypass set -e exit [OK]
Quick Trick: Add || true to ignore errors on specific commands [OK]
Common Mistakes:
MISTAKES
  • Removing set -e loses error protection
  • Using invalid ls -e option
  • Trying set +e disables exit globally, not selective

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes