1TRAP(P)                    POSIX Programmer's Manual                   TRAP(P)
2
3
4

NAME

6       trap - trap signals
7

SYNOPSIS

9       trap [action condition ...]
10

DESCRIPTION

12       If  action is '-' , the shell shall reset each condition to the default
13       value. If action is null ( "" ), the shell shall ignore each  specified
14       condition  if  it  arises. Otherwise, the argument action shall be read
15       and executed by the shell when  one  of  the  corresponding  conditions
16       arises.  The  action  of  trap shall override a previous action (either
17       default action or one explicitly set). The value of "$?" after the trap
18       action completes shall be the value it had before trap was invoked.
19
20       The  condition  can be EXIT, 0 (equivalent to EXIT), or a signal speci‐
21       fied using a symbolic name, without the SIG prefix, as  listed  in  the
22       tables  of  signal  names  in the <signal.h> header defined in the Base
23       Definitions volume of IEEE Std 1003.1-2001, Chapter  13,  Headers;  for
24       example,  HUP,  INT,  QUIT, TERM. Implementations may permit names with
25       the SIG prefix or ignore case in signal names as an extension.  Setting
26       a trap for SIGKILL or SIGSTOP produces undefined results.
27
28       The  environment  in  which  the shell executes a trap on EXIT shall be
29       identical to the environment immediately after the  last  command  exe‐
30       cuted before the trap on EXIT was taken.
31
32       Each  time trap is invoked, the action argument shall be processed in a
33       manner equivalent to:
34
35
36              eval action
37
38       Signals that were ignored on entry to a non-interactive shell cannot be
39       trapped or reset, although no error need be reported when attempting to
40       do so. An interactive shell may  reset  or  catch  signals  ignored  on
41       entry.  Traps  shall remain in place for a given shell until explicitly
42       changed with another trap command.
43
44       When a subshell is entered, traps that are not being ignored are set to
45       the  default  actions. This does not imply that the trap command cannot
46       be used within the subshell to set new traps.
47
48       The trap command with no arguments shall write  to  standard  output  a
49       list of commands associated with each condition. The format shall be:
50
51
52              "trap -- %s %s ...\n", <action>, <condition> ...
53
54       The shell shall format the output, including the proper use of quoting,
55       so that it is suitable for  reinput  to  the  shell  as  commands  that
56       achieve the same trapping results. For example:
57
58
59              save_traps=$(trap)
60              ...
61              eval "$save_traps"
62
63       XSI-conformant systems also allow numeric signal numbers for the condi‐
64       tions corresponding to the following signal names:
65
66
67                             Signal Number   Signal Name
68                             1               SIGHUP
69                             2               SIGINT
70                             3               SIGQUIT
71                             6               SIGABRT
72                             9               SIGKILL
73                             14              SIGALRM
74                             15              SIGTERM
75
76       The trap special built-in shall conform to the Base Definitions  volume
77       of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
78

OPTIONS

80       None.
81

OPERANDS

83       See the DESCRIPTION.
84

STDIN

86       Not used.
87

INPUT FILES

89       None.
90

ENVIRONMENT VARIABLES

92       None.
93

ASYNCHRONOUS EVENTS

95       Default.
96

STDOUT

98       See the DESCRIPTION.
99

STDERR

101       The standard error shall be used only for diagnostic messages.
102

OUTPUT FILES

104       None.
105

EXTENDED DESCRIPTION

107       None.
108

EXIT STATUS

110       If the trap name    or number  is invalid, a non-zero exit status shall
111       be returned; otherwise, zero shall be returned.  For  both  interactive
112       and  non-interactive  shells, invalid signal names    or numbers  shall
113       not be considered a syntax error and do not cause the shell to abort.
114

CONSEQUENCES OF ERRORS

116       Default.
117
118       The following sections are informative.
119

APPLICATION USAGE

121       None.
122

EXAMPLES

124       Write out a list of all traps and actions:
125
126
127              trap
128
129       Set a trap so the logout utility in the directory referred  to  by  the
130       HOME environment variable executes when the shell terminates:
131
132
133              trap '$HOME/logout' EXIT
134
135       or:
136
137
138              trap '$HOME/logout' 0
139
140       Unset traps on INT, QUIT, TERM, and EXIT:
141
142
143              trap - INT QUIT TERM EXIT
144

RATIONALE

146       Implementations  may  permit  lowercase  signal  names as an extension.
147       Implementations may also accept the names with the SIG prefix; no known
148       historical shell does so. The trap and kill utilities in this volume of
149       IEEE Std 1003.1-2001 are now consistent in their omission  of  the  SIG
150       prefix  for  signal  names.  Some kill implementations do not allow the
151       prefix, and kill -l lists the signals without prefixes.
152
153       Trapping SIGKILL or SIGSTOP is syntactically accepted by some  histori‐
154       cal  implementations, but it has no effect. Portable POSIX applications
155       cannot attempt to trap these signals.
156
157       The output format is not historical practice. Since the output of  his‐
158       torical  trap  commands  is not portable (because numeric signal values
159       are not portable) and had to change to become so,  an  opportunity  was
160       taken  to  format  the output in a way that a shell script could use to
161       save and then later reuse a trap if it wanted.
162
163       The KornShell uses an ERR trap that is triggered whenever set -e  would
164       cause an exit. This is allowable as an extension, but was not mandated,
165       as other shells have not used it.
166
167       The text about the environment for the EXIT trap invalidates the behav‐
168       ior  of some historical versions of interactive shells which, for exam‐
169       ple, close the standard input before executing a trap on 0.  For  exam‐
170       ple,  in  some historical interactive shell sessions the following trap
171       on 0 would always print "--" :
172
173
174              trap 'read foo; echo "-$foo-"' 0
175

FUTURE DIRECTIONS

177       None.
178

SEE ALSO

180       Special Built-In Utilities
181
183       Portions of this text are reprinted and reproduced in  electronic  form
184       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
185       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
186       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
187       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
188       event of any discrepancy between this version and the original IEEE and
189       The Open Group Standard, the original IEEE and The Open Group  Standard
190       is  the  referee document. The original Standard can be obtained online
191       at http://www.opengroup.org/unix/online.html .
192
193
194
195IEEE/The Open Group                  2003                              TRAP(P)
Impressum