|
The The most common signal is EXIT, which fires when the script ends for any reason - normal completion, error, or interrupt. |
|
|
Output:
Script is running... About to exit... Goodbye! |
|
|
Scripts often create temporary files that must be cleaned up. Without trap, if a script crashes or is interrupted, temp files leak. With trap, cleanup runs automatically on exit. |
|
|
Output:
Created: /tmp/demo_6 File exists: yes Exiting - trap will clean up... |
|
|
Traps are also available in subshells, and will fire when the specified signal is received for that subshell. |
|
|
Output:
Script is running... About to exit... Goodbye! |
|
|
Common signals
|
|
|
Output:
EXIT signal: Trap set for EXIT EXIT signal caught INT signal (Ctrl+C): Trap set for Ctrl+C, won't run as script ends normally TERM signal (kill): Trap set for TERM, won't run as script ends normally |
|
|
You can trap multiple signals with the same handler by listing them after the command. |
|
|
Output:
Same handler for INT and TERM Exiting normally (neither signal sent) |
|
|
Traps can run multiple commands by separating them with semicolons. |
|
|
Output:
Working... Saving... Closing... |
|
|
For complex cleanup, use a function instead of inline commands. This keeps the trap readable and allows multi-step cleanup. |
|
|
Output:
Created: /tmp/tmp.stab000000 Working with file... Cleanup done |
|
|
EXIT traps run even when scripts exit with an error. This ensures cleanup happens regardless of how the script ends. |
|
|
Output:
About to exit with error... Cleanup ran |
|
|
Variables in traps are evaluated when the trap runs, not when it’s defined. Use single quotes to get this delayed evaluation. |
|
|
Output:
Value is now: changed At exit: changed |
|
|
Inside a trap, $? contains the exit code that triggered the trap. This lets you log or react to how the script exited. Traps don’t change the script’s exit code. The original exit status is preserved after the trap runs. |
|
|
Output:
Exiting with code 42... Trap saw exit code: 42 |
|
|
Subshell traps are independent of the parent shell. A trap set in a subshell doesn’t affect the parent, and vice versa. |
|
|
Output:
In subshell... Child trap Back in parent... Parent trap cleared |
|
|
Setting a new trap replaces the previous one for that signal. Only one trap handler can be active per signal. |
|
|
Output:
First trap set Second trap set (replaces first) Second handler |
|
|
Use |
|
|
Output:
Reset trap: Custom trap set Trap reset (no output on exit) Ignore signal: Ctrl+C is now ignored Ctrl+C restored |
|
|
Bash
Use |
|
|
Output:
Current traps: trap -- 'echo cleanup' EXIT trap -- 'echo interrupted' SIGINT |
|
|
Bash
Bash adds |
|
|
Output:
Running commands... ERR: false failed with status 1 ERR: test -d /nonexistent failed with status 1 Script continues after errors |
|
|
Bash
Bash adds |
|
|
Output:
TRACE: x=5 TRACE: y=10 TRACE: sum=$((x + y)) TRACE: trap - DEBUG Tracing disabled, sum=15 |
|
|
Bash
Bash adds |
|
|
Output:
Calling function: Inside function RETURN: function finished Back in main script |
|
|
Lock files prevent multiple instances of a script from running. The trap ensures the lock is released even if the script fails. |
|
|
Output:
Lock acquired: /tmp/myapp_lock_7 Work complete Lock released: /tmp/myapp_lock_7 |
|
|
Use a flag variable to gracefully exit loops when interrupted, allowing the current iteration to complete. |
|
|
Output:
Processing item 1 Processing item 2 Processing item 3 Graceful shutdown complete |
|
|
Log cleanup actions to help debug issues. Check return values to report success or failure. |
|
|
Output:
Created: /tmp/tmp.stab000000 Files: file1 file2 Exiting... Cleanup: success |
|
|
Set the trap before creating resources. This ensures cleanup runs even if resource creation fails partway through. |
|
|
Output:
Created: /tmp/tmp.stab000000 Working with file... Cleanup complete |
|
|
Handle EXIT for normal cleanup, plus INT (Ctrl+C) and TERM (kill) to ensure cleanup runs on interruption. |
|
|
Output:
Script handles EXIT, INT, and TERM Cleanup always runs via EXIT trap Cleanup ran |
|
|
Track resources in variables so the cleanup function knows what to clean up, even if resources are created dynamically. |
|
|
Output:
Tracking: /tmp/tmp.stab000000 /tmp/tmp.stab000001 Cleaning up /tmp/tmp.stab000000 Cleaning up /tmp/tmp.stab000001 All resources cleaned |
|