trap
Execute commands upon receiving signals
TLDR
List the commands and the names of the expected events
Execute a command when a signal is received
Remove commands
SYNOPSIS
trap [-lp] [[arg] sigspec ...]
PARAMETERS
arg
The command(s) to be executed when the signal(s) sigspec are received. If arg is absent, each specified signal is reset to its original disposition. If arg is the null string the specified signal is ignored by the shell and by invoked commands.
sigspec
The signal specification. This can be a signal name (e.g., SIGINT), a signal number (e.g., 2), or EXIT (or 0) to execute arg when the shell exits.
-l
List signal names and their corresponding numbers.
-p
Print the trap commands associated with each sigspec. If no sigspec is present, print all trap commands.
DESCRIPTION
The trap command in Linux is used to specify commands that should be executed upon receiving signals. Signals are asynchronous notifications sent to a process to indicate events such as termination, interrupt, or illegal instruction. trap allows you to define custom handlers for these signals, enabling graceful shutdowns, cleanup operations, or specific actions upon signal receipt. Without a trap, the default signal handling mechanism of the shell or program applies, which often involves terminating the process.
It's crucial for robust scripting, preventing data loss by executing commands to save progress or release resources before exiting. For example, trap 'rm -f /tmp/mytempfile' EXIT will remove /tmp/mytempfile when the script exits regardless of the reason (normal exit, signal, etc.). This ensures temporary files are cleaned up. You can reset a trapped signal to its default behavior by trapping with an empty command. You can also ignore signals completely. The trap command gives shell scripts increased control over signal handling.
CAVEATS
Signals can be blocked or ignored. Trapped commands are executed in the context of the current shell; variable assignments or directory changes will affect the current shell environment. Certain signals, such as SIGKILL (9) and SIGSTOP (19), cannot be trapped or ignored.
SIGNAL NAMES AND NUMBERS
Signals are identified by both a name (e.g., SIGINT) and a number (e.g., 2). The trap -l command lists the available signals and their numbers. Common signals include SIGINT (interrupt, typically Ctrl+C), SIGTERM (termination request), SIGHUP (hangup, often triggered when a terminal disconnects), and SIGKILL (forced termination).
EXIT TRAP
Using trap with EXIT (or 0) allows you to execute commands when the script exits, regardless of the reason for exiting (normal completion, signal, error). This is useful for cleaning up temporary files or restoring settings.
HISTORY
The trap command has been a standard feature of Unix shells (including the Bourne shell, Bash, and others) for a long time. It was introduced to provide a way to handle asynchronous events and ensure that scripts could respond appropriately to signals. The core functionality has remained largely unchanged over the years, although specific implementations might have slight differences in behavior or supported options.
SEE ALSO
kill(1), signal(7)