O'Reilly logo

Linux in a Nutshell, 6th Edition by Robert Love, Stephen Figgins, Ellen Siever, Arnold Robbins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

trap

Synopsis

trap [options] [[commands] signals]

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

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

Options

-l

List all signals and their numbers, like kill -l.

-p

Print the current trap settings in a form suitable for rereading later.

Signals

A list of signal names, numbers, and meanings were given earlier in the kill entry; see the listing there. 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required