Bird
0
0

What is wrong with this bash script snippet that attempts to trap the EXIT signal?

medium📝 Debug Q6 of 15
Bash Scripting - Error Handling
What is wrong with this bash script snippet that attempts to trap the EXIT signal?
cleanup() {
  echo "Performing cleanup"
}
trap cleanup EXIT
AThe trap command is placed before the cleanup function is defined
BThe cleanup function must be exported to be used in trap
CThe EXIT signal cannot be trapped in bash
DThe trap command should use double quotes around cleanup
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the order of commands

    The trap command references the cleanup function but does not quote it.
  2. Step 2: Understand trap syntax

    Commands in trap should be quoted to be executed properly; missing quotes cause trap to misinterpret the command.
  3. Final Answer:

    The trap command should use double quotes around cleanup -> Option D
  4. Quick Check:

    Commands in trap must be quoted [OK]
Quick Trick: Quote commands in trap to ensure proper execution [OK]
Common Mistakes:
MISTAKES
  • Placing trap before function definition
  • Using incorrect quotes around function name
  • Assuming EXIT cannot be trapped

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes