Foundations Updated 2026-09 View as Markdown

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

shell
#!/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       
done

Surely you can substitute the "echo Booh!" with a function:

shell
function booh {
	echo "booh!"
}

and call it in trap:

shell
trap booh SIGINT SIGTERM

Some 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:

shell
kill -l

Notice the numbers before each signal name, you can use that number to avoid typing long strings in trap:

shell
#2 corresponds to SIGINT and 15 corresponds to SIGTERM
trap booh 2 15

one of the common usage of trap is to do cleanup temporary files:

shell
trap "rm -f folder; exit" 2

Get 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.

Unsubscribe in one click. We never sell the list. Or just take the AGENTS.md now — no email needed.