Name

trap

Synopsis

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

Execute commands if any signals are received. The second form is specific to Bash and ksh93; it prints the current trap settings in a form suitable for rereading later. The third form is specific to Bash; it 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. Bash and ksh93: 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 in Chapter 2. The shells allow you to use either the signal number, or the signal name (without the SIG prefix). In addition, the shells support “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.

KEYBD

A key has been read in emacs, gmacs, or vi editing mode. ksh93 ...

Get Unix in a Nutshell, 4th Edition 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.