0
0
Bash Scriptingscripting~10 mins

Signal handling with trap in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Signal handling with trap
Start script
Set trap for signal
Run main loop or commands
Signal received?
NoContinue running
Yes
Execute trap command
Continue or exit script
The script sets a trap to catch signals, runs commands, and when a signal arrives, it runs the trap command before continuing or exiting.
Execution Sample
Bash Scripting
trap "echo 'Signal received, exiting'; exit" SIGINT
while true; do
  echo "Running..."
  sleep 1
done
This script prints 'Running...' every second until you press Ctrl+C, which triggers the trap to print a message and exit.
Execution Table
StepActionSignal ReceivedTrap Command ExecutedOutputScript State
1Start script and set trapNoNoRunning
2Print 'Running...'NoNoRunning...Running
3Sleep 1 secondNoNoRunning
4Print 'Running...'NoNoRunning...Running
5Sleep 1 secondNoNoRunning
6User presses Ctrl+C (SIGINT)YesYesSignal received, exitingExiting
7Exit scriptNoNoExited
💡 User pressed Ctrl+C, SIGINT caught, trap executed, script exited
Variable Tracker
VariableStartAfter 1After 2After SIGINTFinal
Script StateNot startedRunningRunningExitingExited
Key Moments - 3 Insights
Why does the script not exit immediately when started?
Because the trap only runs when the specified signal (SIGINT) is received. Until then, the script keeps running the loop as shown in rows 2-5 of the execution_table.
What happens when Ctrl+C is pressed during the sleep?
The signal SIGINT is sent, the trap command runs (row 6), printing the message and exiting the script immediately.
Can the trap command run multiple times if multiple signals are sent?
Yes, each time the signal is received, the trap command runs. But in this example, the trap exits the script, so it runs only once.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the script state after step 4?
AExiting
BExited
CRunning
DNot started
💡 Hint
Check the 'Script State' column at step 4 in the execution_table
At which step does the trap command execute?
AStep 6
BStep 4
CStep 2
DStep 7
💡 Hint
Look for 'Trap Command Executed' marked 'Yes' in the execution_table
If the trap command did not call exit, what would happen after step 6?
AScript would exit immediately
BScript would continue running the loop
CScript would pause indefinitely
DScript would restart
💡 Hint
Refer to the key_moments explanation about trap behavior without exit
Concept Snapshot
trap "command" SIGNAL
- Sets a command to run when SIGNAL is received
- Commonly used to handle Ctrl+C (SIGINT)
- Allows cleanup or graceful exit
- Script continues unless trap command exits
- Example: trap "echo Exiting; exit" SIGINT
Full Transcript
This visual trace shows how a bash script uses trap to handle signals. The script sets a trap for SIGINT (Ctrl+C). It runs a loop printing 'Running...' every second. When the user presses Ctrl+C, the signal is caught, the trap command runs printing 'Signal received, exiting', then the script exits. Variables like script state change from running to exiting to exited. Key moments include understanding that the trap only runs on signal, and that exit in the trap stops the script. The quiz tests understanding of script state and trap execution steps.