What is the purpose of the trap command in bash scripting?
The trap command lets a script catch signals (like Ctrl+C) and run specific commands before exiting or continuing. It helps scripts handle interruptions safely.
Click to reveal answer
beginner
How do you use trap to run a cleanup function when the script receives a SIGINT (Ctrl+C)?
You write: trap cleanup_function SIGINT. This means when the script gets SIGINT, it runs cleanup_function before stopping.
Click to reveal answer
beginner
What happens if you don’t use trap and press Ctrl+C during a bash script?
The script stops immediately without running any cleanup. This might leave temporary files or processes running.
Click to reveal answer
intermediate
Explain the syntax of the trap command.
The syntax is: trap 'commands' SIGNALS. You put the commands to run in quotes, then list one or more signals to catch.
Click to reveal answer
intermediate
Can trap handle multiple signals at once? How?
Yes. You list multiple signals after the commands, like: trap 'echo Signal caught' SIGINT SIGTERM. The commands run if any listed signal occurs.
Click to reveal answer
What does the trap command do in a bash script?
ACatches signals and runs commands before exiting
BStarts a new background process
CCreates a new file
DChanges file permissions
✗ Incorrect
The trap command catches signals like Ctrl+C and runs specified commands before the script exits or continues.
Which signal is sent when you press Ctrl+C in the terminal?
ASIGINT
BSIGKILL
CSIGHUP
DSIGTERM
✗ Incorrect
Ctrl+C sends the SIGINT (interrupt) signal to the running process.
How do you remove a previously set trap in bash?
Atrap - SIGNAL
Btrap '' SIGNAL
Ctrap reset SIGNAL
Dtrap clear SIGNAL
✗ Incorrect
Using trap '' SIGNAL removes the trap for that signal.
What will this command do? trap 'echo Exiting' EXIT
APrint 'Exiting' when Ctrl+C is pressed
BRestart the script
CIgnore all signals
DPrint 'Exiting' when the script ends
✗ Incorrect
The EXIT signal runs when the script finishes, so it prints 'Exiting' at the end.
If a script traps SIGTERM and SIGINT with the same command, what happens when either signal is received?
AThe command runs only for SIGINT
BThe command runs only for SIGTERM
CThe command runs for both signals
DThe script ignores both signals
✗ Incorrect
The trap command runs the specified commands for any of the listed signals.
Describe how to use the trap command to handle a signal and why it is useful in bash scripts.
Think about what happens when you press Ctrl+C and how trap can help.
You got /4 concepts.
Explain what happens if a bash script does not use trap and receives an interrupt signal.
Consider what happens when you stop a program suddenly.
You got /3 concepts.
Practice
(1/5)
1. What does the trap command do in a bash script?
easy
A. It creates a loop that repeats commands indefinitely.
B. It catches signals like Ctrl+C and runs specified commands.
C. It defines a function to be used later in the script.
D. It pauses the script until a key is pressed.
Solution
Step 1: Understand the purpose of trap
The trap command is used to catch signals sent to the script, such as Ctrl+C (SIGINT).
Step 2: What trap does with signals
When a signal is caught, trap runs the commands specified by the user to handle that signal.
Final Answer:
It catches signals like Ctrl+C and runs specified commands. -> Option B
Quick Check:
trap catches signals = B [OK]
Hint: Trap runs commands on signals like Ctrl+C [OK]
Common Mistakes:
Thinking trap creates loops
Confusing trap with function definition
Assuming trap pauses script
2. Which of the following is the correct syntax to run echo "Bye" when the script receives a SIGINT signal?
easy
A. trap 'echo "Bye"' SIGINT
B. trap echo "Bye" SIGINT
C. trap (echo "Bye") SIGINT
D. trap {echo "Bye"} SIGINT
Solution
Step 1: Check trap syntax for commands
The commands to run must be quoted as a single string, usually with single quotes to preserve special characters.
Step 2: Identify correct quoting style
trap 'echo "Bye"' SIGINT uses single quotes around the entire command string, which is the correct syntax.
Final Answer:
trap 'echo "Bye"' SIGINT -> Option A
Quick Check:
trap 'commands' SIGNAL = A [OK]
Hint: Use single quotes around commands in trap [OK]
Common Mistakes:
Not quoting commands properly
Using parentheses or braces incorrectly
Omitting quotes causing syntax errors
3. What will be the output of this script when you press Ctrl+C during its execution?
#!/bin/bash
trap 'echo "Interrupted! Cleaning up..."; exit' SIGINT
while true; do
echo "Running..."
sleep 2
done
medium
A. The script ignores Ctrl+C and keeps running.
B. It crashes with an error message.
C. It prints "Running..." once and then exits.
D. It prints "Interrupted! Cleaning up..." and exits.
Solution
Step 1: Understand trap on SIGINT
The trap command catches the SIGINT signal (Ctrl+C) and runs the echo and exit commands.
Step 2: Effect of trap during infinite loop
When Ctrl+C is pressed, the script prints the message and exits immediately, stopping the loop.
Final Answer:
It prints "Interrupted! Cleaning up..." and exits. -> Option D
Quick Check:
trap on SIGINT prints message and exits = D [OK]
Hint: trap runs commands on Ctrl+C, then script exits [OK]
Common Mistakes:
Thinking script ignores Ctrl+C
Assuming loop stops without message
Expecting error instead of clean exit
4. Identify the error in this script snippet:
trap echo "Exiting" SIGTERM
sleep 10
medium
A. sleep command cannot be used with trap.
B. SIGTERM is not a valid signal name.
C. Missing quotes around the command to run in trap.
D. trap must be placed after sleep command.
Solution
Step 1: Check trap command syntax
The command to run on signal must be quoted as a single string to avoid syntax errors.
Step 2: Identify missing quotes
Here, echo "Exiting" is not quoted, so the shell misinterprets the command.
Final Answer:
Missing quotes around the command to run in trap. -> Option C
Quick Check:
trap commands need quotes = C [OK]
Hint: Always quote trap commands to avoid syntax errors [OK]
Common Mistakes:
Not quoting trap commands
Using invalid signal names
Placing trap after commands needing it
5. You want a bash script to delete a temporary file /tmp/mytempfile when it exits, whether normally or due to Ctrl+C. Which trap command correctly achieves this?
hard
A. trap 'rm -f /tmp/mytempfile' EXIT
B. trap 'rm -f /tmp/mytempfile' SIGINT
C. trap 'rm -f /tmp/mytempfile' SIGTERM
D. trap 'rm -f /tmp/mytempfile' SIGINT SIGTERM
Solution
Step 1: Understand EXIT signal
EXIT is a special trap signal that runs commands when the script ends for any reason, including normal exit or Ctrl+C.
Step 2: Why EXIT is better than SIGINT or SIGTERM alone
Using EXIT ensures cleanup runs on all script exits, not just specific signals.
Final Answer:
trap 'rm -f /tmp/mytempfile' EXIT -> Option A
Quick Check:
Use EXIT to clean up on all exits = A [OK]
Hint: Use trap on EXIT to clean up on any script exit [OK]
Common Mistakes:
Using only SIGINT or SIGTERM misses normal exit
Not quoting the command
Expecting multiple signals needed for all exits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.