Name

trap

Synopsis

trap [ [commands] signals]
trap -p
trap -l

Execute commands if any signals are received. The second form prints the current trap settings in a form suitable for rereading later. The third form lists all signals and their numbers, like kill -l.

Common signals include EXIT (0), HUP (1), INT (2), and TERM (15). Multiple commands must be quoted as a group and separated by semicolons internally. If commands is the null string (i.e., trap "" signals), signals are ignored by the shell. If commands are omitted entirely, reset processing of specified signals to the default action. If commands is "-", reset signals to their initial defaults.

If both commands and signals are omitted, list current trap assignments. See the Examples here and in exec.

Signals

A list of signal names, numbers, and meanings were given earlier in the kill entry. The shell allows you to use either the signal number or the signal name (without the SIG prefix). In addition, the shell supports "pseudo-signals," signal names or numbers that aren't real operating system signals but which direct the shell to perform a specific action. These signals are:

DEBUG

Execution of any command.

ERR

Nonzero exit status.

EXIT

Exit from shell (usually when shell script finishes).

0

Same as EXIT, for historical compatibility with the Bourne shell.

RETURN

A return is executed, or a script run with . (dot) or source finishes.

Examples

trap "" INT     Ignore interrupts (signal 2)
trap INT        Obey interrupts again

Remove a ...

Get bash Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.