'\" te
.\" Copyright (c) 2007 Sun Microsystems, Inc. - All Rights Reserved.
.\" Copyright 1989
.\" Portions Copyright (c) 1982-2007 AT&T Knowledge Ventures
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH TRAP 1 "Nov 20, 2007"
.SH NAME
trap, onintr \- shell built-in functions to respond to (hardware) signals
.SH SYNOPSIS
.SS "sh"
.LP
.nf
\fBtrap\fR [\fIargument\fR \fIn\fR [\fIn2\fR]...]
.fi

.SS "csh"
.LP
.nf
\fBonintr\fR [\fB-|\fR \fIlabel\fR]
.fi

.SS "ksh"
.LP
.nf
\fB*trap\fR [\fIarg\fR \fIsig\fR [\fIsig2\fR...]]
.fi

.SS "ksh93"
.LP
.nf
\fB+trap\fR [\fB-p\fR] [\fIaction\fR \fIcondition\fR...]
.fi

.SH DESCRIPTION
.SS "sh"
.sp
.LP
The \fBtrap\fR command \fIargument\fR is to be read and executed when the shell
receives numeric or symbolic signal(s) (\fIn\fR). (Note: \fIargument\fR is
scanned once when the trap is set and once when the trap is taken.) Trap
commands are executed in order of signal number or corresponding symbolic
names. Any attempt to set a trap on a signal that was ignored on entry to the
current shell is ineffective. An attempt to trap on signal 11 (memory fault)
produces an error. If \fIargument\fR is absent all trap(s) \fIn\fR are reset to
their original values. If \fIargument\fR is the null string this signal is
ignored by the shell and by the commands it invokes. If \fIn\fR is \fB0\fR the
command \fIargument\fR is executed on exit from the shell. The \fBtrap\fR
command with no arguments prints a list of commands associated with each signal
number.
.SS "csh"
.sp
.LP
\fBonintr\fR controls the action of the shell on interrupts. With no arguments,
\fBonintr\fR restores the default action of the shell on interrupts. (The shell
terminates shell scripts and returns to the terminal command input level). With
the \fB\(mi\fR argument, the shell ignores all interrupts. With a \fIlabel\fR
argument, the shell executes a \fBgoto\fR \fIlabel\fR when an interrupt is
received or a child process terminates because it was interrupted.
.SS "ksh"
.sp
.LP
\fBtrap\fR uses \fIarg\fR as a command to be read and executed when the shell
receives signal(s) \fIsig\fR. \fIarg\fR is scanned once when the trap is set
and once when the trap is taken. Each \fIsig\fR can be specified as a number or
as the name of the signal. \fBtrap\fR commands 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 \fIarg\fR is omitted or is \fB\(mi\fR, then
the trap(s) for each \fIsig\fR are reset to their original values. If \fIarg\fR
is the null (the empty string, for example, \fB""\fR) string then this signal
is ignored by the shell and by the commands it invokes. If \fIsig\fR is
\fBERR\fR then \fIarg\fR are executed whenever a command has a non-zero exit
status. If \fIsig\fR is \fBDEBUG\fR then \fIarg\fR are executed after each
command. If \fIsig\fR is \fB0\fR or \fBEXIT\fR for a \fBtrap\fR set outside any
function then the command \fIarg\fR is executed on exit from the shell. The
\fBtrap\fR command with no arguments prints a list of commands associated with
each signal number.
.sp
.LP
On this manual page, \fBksh\fR(1) commands that are preceded by one or two *
(asterisks) are treated specially in the following ways:
.RS +4
.TP
1.
Variable assignment lists preceding the command remain in effect when the
command completes.
.RE
.RS +4
.TP
2.
I/O redirections are processed after variable assignments.
.RE
.RS +4
.TP
3.
Errors cause a script that contains them to abort.
.RE
.RS +4
.TP
4.
Words, following a command preceded by ** that are in the format of a
variable assignment, are expanded with the same rules as a variable assignment.
This means that tilde substitution is performed after the \fB=\fR sign and word
splitting and file name generation are not performed.
.RE
.SS "ksh93"
.sp
.LP
\fBtrap\fR is a special built-in that defines actions to be taken when
conditions such as receiving a signal occur. \fBtrap\fR can also be used to
display the current trap settings on standard output.
.sp
.LP
If \fIaction\fR is \fB-\fR, \fBtrap\fR resets each condition to the default
value. If \fIaction\fR is an empty string, the shell ignores each of the
conditions if they arise. Otherwise, the argument \fIaction\fR is read and
executed by the shell as if it were processed by \fBeval\fR when one of the
corresponding conditions arise. The action of the trap overrides any previous
action associated with each specified condition. The value of \fB$?\fR is not
altered by the trap execution.
.sp
.LP
\fIcondition\fR can be the name or number of a signal, or one of the following:
.sp
.ne 2
.na
\fB\fBEXIT\fR\fR
.ad
.RS 9n
Execute this trap when the shell exits. If defined within a function with the
\fBfunction\fR reserved word, executes the trap in the caller's environment
when the function returns. The trap action is restored to the value it had when
it called the function.
.RE

.sp
.ne 2
.na
\fB\fB0\fR\fR
.ad
.RS 9n
Same as \fBEXIT\fR.
.RE

.sp
.ne 2
.na
\fB\fBDEBUG\fR\fR
.ad
.RS 9n
Execute before each simple command is executed but after the arguments are
expanded.
.RE

.sp
.ne 2
.na
\fB\fBERR\fR\fR
.ad
.RS 9n
Execute whenever \fBset -e\fR would cause the shell to exit.
.RE

.sp
.ne 2
.na
\fB\fBKEYBD\fR\fR
.ad
.RS 9n
Execute when a key is entered from a terminal device.
.RE

.sp
.LP
Signal names are case insensitive and the \fBsig\fR prefix is optional. Signals
that were ignored on entry to a non-interactive shell cannot trapped or reset
although doing so does not report an error. The use of signal numbers other
than 1, 2, 3, 6, 9, 14, and 15 are not portable.
.sp
.LP
Although trap is a special built-in, specifying a condition that the shell does
not know about causes trap to exit with a \fBnon-zero\fR exit status, but does
not terminate the invoking shell.
.sp
.LP
If no action or conditions are specified then all the current trap settings are
written to standard output.
.sp
.LP
The following options are supported by the \fBtrap\fR built-in command in
\fBksh93\fR:
.sp
.ne 2
.na
\fB\fB-p\fR\fR
.ad
.RS 6n
Causes the current traps to be output in a format that can be processed as
input to the shell to recreate the current traps.
.RE

.sp
.LP
The trap built-in in \fBksh93\fR exits with one of the following values:
.sp
.ne 2
.na
\fB\fB0\fR\fR
.ad
.RS 6n
Successful completion.
.RE

.sp
.ne 2
.na
\fB\fB>0\fR\fR
.ad
.RS 6n
An error occurred.
.RE

.sp
.LP
On this manual page, \fBksh93\fR(1) commands that are preceded by one or two
\fB+\fR (plus signs) are treated specially in the following ways:
.RS +4
.TP
1.
Variable assignment lists preceding the command remain in effect when the
command completes.
.RE
.RS +4
.TP
2.
I/O redirections are processed after variable assignments.
.RE
.RS +4
.TP
3.
Errors cause a script that contains them to abort.
.RE
.RS +4
.TP
4.
They are not valid function names.
.RE
.RS +4
.TP
5.
Words, following a command preceded by \fB++\fR that are in the format of a
variable assignment, are expanded with the same rules as a variable assignment.
This means that tilde substitution is performed after the \fB=\fR sign and word
splitting and file name generation are not performed.
.RE
.SH SEE ALSO
.sp
.LP
\fBcsh\fR(1), \fBeval\fR(1), \fBexit\fR(1), \fBksh\fR(1), \fBksh93\fR(1),
\fBsh\fR(1), \fBattributes\fR(5)