The trap command lets you run a specific command or script when the shell receives certain signals or when the script exits. It is often used to clean up temporary files or reset settings.
Click to reveal answer
beginner
How do you use trap to run a cleanup function when a script exits?
You write a cleanup function and then use trap cleanup EXIT. This means the cleanup function runs automatically when the script finishes or is interrupted.
Click to reveal answer
intermediate
Why is it useful to trap the EXIT signal in a script?
Trapping EXIT ensures that cleanup code runs no matter how the script ends, whether normally or due to an error or interruption. This helps keep your system clean and avoids leftover temporary files.
Click to reveal answer
intermediate
What happens if you trap signals like INT or TERM in a script?
Trapping INT (interrupt) or TERM (terminate) lets your script handle those signals gracefully, such as cleaning up before exiting instead of stopping immediately.
Click to reveal answer
beginner
Show a simple example of a bash script that creates a temp file and deletes it on exit using trap.
What signal does trap cleanup EXIT catch in a bash script?
AKeyboard interrupt only
BSystem shutdown
CScript exit (normal or interrupted)
DFile deletion
✗ Incorrect
The EXIT signal triggers when the script ends for any reason, normal or interrupted.
Which command creates a temporary file in bash?
Atempfile
Bmktemp
Ctouch temp
Dcreatefile
✗ Incorrect
mktemp safely creates a unique temporary file.
Why use trap to catch signals like INT or TERM?
ATo run cleanup code before the script stops
BTo speed up the script
CTo ignore errors
DTo create new files
✗ Incorrect
Trapping these signals lets the script clean up before exiting.
What happens if you don’t use trap for cleanup in a script that creates temp files?
AFiles are deleted automatically
BScript runs faster
CScript won’t run
DTemp files may remain and clutter the system
✗ Incorrect
Without cleanup, temporary files can stay and waste space.
Which of these is a correct way to set a trap for cleanup on exit?
Atrap cleanup EXIT
Btrap EXIT cleanup
Ctrap cleanup SIGINT
Dtrap EXIT
✗ Incorrect
The syntax is trap <command> <signal>, so trap cleanup EXIT is correct.
Explain how the trap command helps with cleanup in bash scripts.
Think about what happens when a script ends or is interrupted.
You got /5 concepts.
Describe a simple bash script scenario where trapping EXIT is important and what it does.
Imagine you create a file that should be deleted no matter how the script ends.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using trap in a bash script?
easy
A. To define functions inside the script
B. To create loops that repeat commands
C. To run specific commands when the script exits or receives signals
D. To declare variables with default values
Solution
Step 1: Understand the role of trap
The trap command is designed to catch signals or script exit events and run commands in response.
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.
Final Answer:
To run specific commands when the script exits or receives signals -> Option C
Quick Check:
trap runs commands on exit/signals = A [OK]
Hint: Trap runs cleanup commands on exit or signals [OK]
Common Mistakes:
Confusing trap with loops or functions
Thinking trap declares variables
Assuming trap runs commands continuously
2. Which of the following is the correct syntax to run cleanup function on script exit using trap?
easy
A. trap EXIT cleanup
B. trap 'cleanup' on EXIT
C. trap cleanup on EXIT
D. trap 'cleanup' EXIT
Solution
Step 1: Recall trap syntax for commands
The command(s) to run must be quoted as a string, followed by the signal or event name.
Step 2: Check each option's syntax
trap 'cleanup' EXIT correctly quotes the command and uses the signal EXIT. trap EXIT cleanup places the signal before the command (wrong order). Options C and D use incorrect keywords like 'on'.
Final Answer:
trap 'cleanup' EXIT -> Option D
Quick Check:
trap 'command' SIGNAL = B [OK]
Hint: Always quote commands in trap before the signal [OK]
Common Mistakes:
Omitting quotes around commands
Using 'on' keyword incorrectly
Placing signal before command
3. What will be the output of this script?
#!/bin/bash
cleanup() {
echo "Cleaning up temp files"
}
trap 'cleanup' EXIT
echo "Script is running"
medium
A. Script is running
B. Script is running\nCleaning up temp files
C. No output
D. Cleaning up temp files
Solution
Step 1: Understand script flow
The script prints "Script is running" first, then exits.
Step 2: Effect of trap on EXIT
On exit, the cleanup function runs, printing "Cleaning up temp files" after the first echo.
Final Answer:
Script is running\nCleaning up temp files -> Option B
Quick Check:
trap runs cleanup on exit, so both lines print [OK]
Hint: trap runs cleanup after script finishes [OK]
Common Mistakes:
Thinking trap runs before script output
Expecting only cleanup message
Ignoring trap on EXIT
4. Identify the error in this script snippet that uses trap for cleanup:
For simple function names like cleanup, quotes are not required around the command in trap.
Step 2: Analyze script order and signals
Defining cleanup after trap is allowed because bash processes function definitions before executing traps. EXIT is valid. No syntax errors present.
Final Answer:
No error, script is correct -> Option A
Quick Check:
unquoted function name OK, post-definition OK [OK]
Hint: trap accepts unquoted simple function names [OK]
Common Mistakes:
Thinking functions must be defined before trap
Believing EXIT is an invalid signal
Assuming quotes are required around simple commands
5. You want to ensure a temporary file /tmp/mytempfile is deleted when your bash script exits, even if interrupted. Which trap command correctly achieves this?
hard
A. trap 'rm -f /tmp/mytempfile' EXIT INT TERM
B. trap rm -f /tmp/mytempfile EXIT INT TERM
C. trap 'rm -f /tmp/mytempfile' ONEXIT
D. trap 'rm -f /tmp/mytempfile' EXIT SIGNAL
Solution
Step 1: Understand trap for multiple signals
To catch script exit and interruptions, use signals EXIT, INT, and TERM together.
Step 2: Check syntax correctness
Commands must be quoted. trap 'rm -f /tmp/mytempfile' EXIT INT TERM quotes the command and lists valid signals. Options B misses quotes, C uses invalid signal ONEXIT, D uses invalid SIGNAL keyword.
Final Answer:
trap 'rm -f /tmp/mytempfile' EXIT INT TERM -> Option A
Quick Check:
trap 'command' SIGNALS with quotes = A [OK]
Hint: Quote commands and list all signals to catch [OK]
Common Mistakes:
Missing quotes around commands
Using invalid signal names
Not including INT and TERM for interrupts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.