Bash trap command
It often comes the situations that you want to catch a special signal/interruption/user input in your script to prevent the unpredictables.
It often comes the situations that you want to catch a special signal/interruption/user input in your script to prevent the unpredictables.
Trap is your command to try:
trap
###Example
#!/bin/bash
# traptest.sh
# notice you cannot make Ctrl-C work in this shell,
# try with your local one, also remeber to chmod +x
# your local .sh file so you can execute it!
trap "echo Booh!" SIGINT SIGTERM
echo "it's going to run until you hit Ctrl+Z"
echo "hit Ctrl+C to be blown away!"
while true
do
sleep 60
doneSurely you can substitute the "echo Booh!" with a function:
function booh {
echo "booh!"
}and call it in trap:
trap booh SIGINT SIGTERMSome of the common signal types you can trap:
SIGINT: user sends an interrupt signal (Ctrl + C)
SIGQUIT: user sends a quit signal (Ctrl + C)
SIGFPE: attempted an illegal mathematical operation
You can check out all signal types by entering the following command:
kill -lNotice the numbers before each signal name, you can use that number to avoid typing long strings in trap:
#2 corresponds to SIGINT and 15 corresponds to SIGTERM
trap booh 2 15one of the common usage of trap is to do cleanup temporary files:
trap "rm -f folder; exit" 2Get the Bash agent pack
A battle-tested AGENTS.md, the review checklist, and the failure-mode cheat sheet for Bash. One email, then occasional updates when the tooling shifts. No course pitch.
AGENTS.md now — no email needed.