Open System Services Shell and Utilities Reference Manual (G06.28+, H06.05+)
User Commands (t - u) trap(1)
NAME
trap - Provides instructions to a process
SYNOPSIS
trap [argument][signal ...]
DESCRIPTION
The trap command provides instructions to a program when signals are received.
The argument variable specifies a command to be read and executed when the shell receives the
specified signals. (Note that argument is scanned once when the trap is set and once when the
trap is taken.) Each signal can be given as a number or as the name of the signal. Trap com-
mands are executed in order of signal number. Any attempt to set a trap on a signal that was
ignored on entry to the current shell is ineffective.
If argument is omitted or is -, all traps are reset to their original values.
If argument is a null string, this signal is ignored by the shell and by the commands it invokes.
If signal is ERR, argument is executed whenever a command has a nonzero exit status.
If signal is DEBUG, argument is executed after each command.
If signal is 0 or EXIT and the trap statement is executed inside the body of a function, the com-
mand argument is executed after the function completes.
If signal is 0 (zero) or EXIT for a trap set outside any function, the command argument is exe-
cuted on exit from the shell. The trap command with no arguments prints a list of commands
associated with each signal number.
NOTES
• Parameter assignment lists that precede the command remain in effect when the com-
mand completes.
• I/O redirections are processed after parameter assignments.
• Errors cause a script that contains the commands so marked to abort.
The trap command is a shell built-in command. It differs from the regular commands in that it
does not open a new shell process when it executes.
A general discussion of shell built-in commands and a list of the OSS shell built-in commands
are in the reference page for sh(1).
RELATED INFORMATION
Commands: sh(1).
527188-007 Hewlett-Packard Company 9−21