Bird
0
0

What is the main purpose of using trap in a bash script?

easy🧠 Conceptual Q11 of 15
Bash Scripting - Error Handling
What is the main purpose of using trap in a bash script?
ATo define functions inside the script
BTo create loops that repeat commands
CTo run specific commands when the script exits or receives signals
DTo declare variables with default values
Step-by-Step Solution
Solution:
  1. Step 1: Understand the role of trap

    The trap command is designed to catch signals or script exit events and run commands in response.
  2. Step 2: Compare options with trap's purpose

    Options B, C, and D describe other bash features unrelated to trap. Only To run specific commands when the script exits or receives signals correctly describes its purpose.
  3. Final Answer:

    To run specific commands when the script exits or receives signals -> Option C
  4. Quick Check:

    trap runs commands on exit/signals = A [OK]
Quick Trick: Trap runs cleanup commands on exit or signals [OK]
Common Mistakes:
MISTAKES
  • Confusing trap with loops or functions
  • Thinking trap declares variables
  • Assuming trap runs commands continuously

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes