Signal handling with trap in Bash Scripting - Time & Space Complexity
When using signal handling with trap in bash scripts, it is important to understand how the script's execution time changes as it runs and handles signals.
We want to know how the script's work grows when signals occur during its run.
Analyze the time complexity of the following bash script using trap.
#!/bin/bash
trap 'echo "Signal received!"' SIGINT
for i in {1..5}; do
echo "Counting: $i"
sleep 1
done
This script counts from 1 to 5, pausing 1 second each time. It listens for the interrupt signal (Ctrl+C) and prints a message when caught.
Look at what repeats and what happens when a signal arrives.
- Primary operation: The
forloop runs 5 times, each with a 1-second pause. - Signal handling: The
trapcommand sets a handler that runs instantly when a signal arrives, interrupting the current operation.
The loop runs a fixed number of times (5), so the main work stays the same no matter what.
| Input Size (n) | Approx. Operations |
|---|---|
| 5 (fixed) | 5 loop cycles + signal checks |
| 10 (if loop changed) | 10 loop cycles + signal checks |
| 100 (if loop changed) | 100 loop cycles + signal checks |
Pattern observation: The loop work grows linearly if the count changes. Signal handling runs only when signals arrive, so it depends on external events, not input size.
Time Complexity: O(n)
This means the script's running time grows directly with the number of loop cycles, while signal handling happens instantly and does not add to the loop's growth.
[X] Wrong: "Signal handling with trap makes the script run slower for every loop cycle."
[OK] Correct: The trap handler only runs when a signal arrives, so it does not slow down the loop itself. The loop runs at its normal speed unless interrupted.
Understanding how signal handling affects script execution helps you write responsive and efficient scripts. This skill shows you can manage asynchronous events without slowing down your main work.
What if the trap handler ran a long command instead of just echoing? How would that affect the time complexity?
Practice
trap command do in a bash script?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 BQuick Check:
trap catches signals = B [OK]
- Thinking trap creates loops
- Confusing trap with function definition
- Assuming trap pauses script
echo "Bye" when the script receives a SIGINT signal?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 AQuick Check:
trap 'commands' SIGNAL = A [OK]
- Not quoting commands properly
- Using parentheses or braces incorrectly
- Omitting quotes causing syntax errors
#!/bin/bash trap 'echo "Interrupted! Cleaning up..."; exit' SIGINT while true; do echo "Running..." sleep 2 done
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 DQuick Check:
trap on SIGINT prints message and exits = D [OK]
- Thinking script ignores Ctrl+C
- Assuming loop stops without message
- Expecting error instead of clean exit
trap echo "Exiting" SIGTERM sleep 10
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 CQuick Check:
trap commands need quotes = C [OK]
- Not quoting trap commands
- Using invalid signal names
- Placing trap after commands needing it
/tmp/mytempfile when it exits, whether normally or due to Ctrl+C. Which trap command correctly achieves this?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 AQuick Check:
Use EXIT to clean up on all exits = A [OK]
- Using only SIGINT or SIGTERM misses normal exit
- Not quoting the command
- Expecting multiple signals needed for all exits
Ready to prove it?
Pass the test and this pattern moves unknown
