Bird
0
0

You want a bash script to stop on any error except for a specific command that might fail but should not stop the script. How do you write this script snippet correctly?

hard🚀 Application Q15 of 15
Bash Scripting - Error Handling
You want a bash script to stop on any error except for a specific command that might fail but should not stop the script. How do you write this script snippet correctly?
#!/bin/bash
set -e
# Your command here
other_command
Which snippet correctly allows your_command to fail without stopping the script?
Ayour_command ; true other_command
Bset +e your_command other_command
Cyour_command || true other_command
Dtrap '' ERR your_command other_command
Step-by-Step Solution
Solution:
  1. Step 1: Understand selective error ignoring

    To allow one command to fail without stopping the script under set -e, append || true to that command.
  2. Step 2: Compare other options

    your_command ; true other_command: your_command ; true fails because set -e exits after your_command before true. set +e your_command other_command disables set -e but doesn't re-enable it for other_command. trap '' ERR your_command other_command ignores ERR trap but doesn't restore it, affecting other_command.
  3. Final Answer:

    your_command || true other_command -> Option C
  4. Quick Check:

    Use command || true to ignore errors selectively [OK]
Quick Trick: Add || true after command to ignore its errors [OK]
Common Mistakes:
MISTAKES
  • Disabling set -e globally instead of locally
  • Using if without handling exit status properly
  • Using traps unnecessarily complicates script

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes