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

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       trap — trap signals
14

SYNOPSIS

16       trap n [condition...]
17       trap [action condition...]
18

DESCRIPTION

20       If the first operand is an unsigned decimal integer,  the  shell  shall
21       treat all operands as conditions, and shall reset each condition to the
22       default value. Otherwise, if there are operands, the first  is  treated
23       as an action and the remaining as conditions.
24
25       If  action  is '−', the shell shall reset each condition to the default
26       value. If action is null (""), the shell shall  ignore  each  specified
27       condition  if  it  arises. Otherwise, the argument action shall be read
28       and executed by the shell when  one  of  the  corresponding  conditions
29       arises.  The  action  of  trap shall override a previous action (either
30       default action or one explicitly set). The value of "$?" after the trap
31       action completes shall be the value it had before trap was invoked.
32
33       The  condition  can be EXIT, 0 (equivalent to EXIT), or a signal speci‐
34       fied using a symbolic name, without the SIG prefix, as  listed  in  the
35       tables  of  signal  names  in the <signal.h> header defined in the Base
36       Definitions volume of POSIX.1‐2008, Chapter 13, Headers;  for  example,
37       HUP,  INT,  QUIT,  TERM.  Implementations may permit names with the SIG
38       prefix or ignore case in signal names as an extension. Setting  a  trap
39       for SIGKILL or SIGSTOP produces undefined results.
40
41       The  environment  in  which  the shell executes a trap on EXIT shall be
42       identical to the environment immediately after the  last  command  exe‐
43       cuted before the trap on EXIT was taken.
44
45       Each  time trap is invoked, the action argument shall be processed in a
46       manner equivalent to:
47
48           eval action
49
50       Signals that were ignored on entry to a non-interactive shell cannot be
51       trapped or reset, although no error need be reported when attempting to
52       do so. An interactive shell may  reset  or  catch  signals  ignored  on
53       entry.  Traps  shall remain in place for a given shell until explicitly
54       changed with another trap command.
55
56       When a subshell is entered, traps that are not being ignored  shall  be
57       set  to  the default actions, except in the case of a command substitu‐
58       tion containing only a single trap command, when the traps need not  be
59       altered.  Implementations  may  check  for this case using only lexical
60       analysis; for example, if `trap` and $( trap --  )  do  not  alter  the
61       traps  in the subshell, cases such as assigning var=trap and then using
62       $($var) may still alter them. This does not imply that the trap command
63       cannot be used within the subshell to set new traps.
64
65       The trap command with no operands shall write to standard output a list
66       of commands associated with each condition. If the command is  executed
67       in  a  subshell, the implementation does not perform the optional check
68       described above for a command substitution  containing  only  a  single
69       trap  command,  and  no  trap commands with operands have been executed
70       since entry to the subshell, the list shall contain the  commands  that
71       were  associated  with  each  condition immediately before the subshell
72       environment was entered.  Otherwise, the list shall  contain  the  com‐
73       mands currently associated with each condition. The format shall be:
74
75           "trap −− %s %s ...\n", <action>, <condition> ...
76
77       The shell shall format the output, including the proper use of quoting,
78       so that it is suitable for  reinput  to  the  shell  as  commands  that
79       achieve the same trapping results. For example:
80
81           save_traps=$(trap)
82           ...
83           eval "$save_traps"
84
85       XSI-conformant systems also allow numeric signal numbers for the condi‐
86       tions corresponding to the following signal names:
87
88       1     SIGHUP
89
90       2     SIGINT
91
92       3     SIGQUIT
93
94       6     SIGABRT
95
96       9     SIGKILL
97
98       14    SIGALRM
99
100       15    SIGTERM
101
102       The trap special built-in shall conform to the Base Definitions  volume
103       of POSIX.1‐2008, Section 12.2, Utility Syntax Guidelines.
104

OPTIONS

106       None.
107

OPERANDS

109       See the DESCRIPTION.
110

STDIN

112       Not used.
113

INPUT FILES

115       None.
116

ENVIRONMENT VARIABLES

118       None.
119

ASYNCHRONOUS EVENTS

121       Default.
122

STDOUT

124       See the DESCRIPTION.
125

STDERR

127       The standard error shall be used only for diagnostic messages.
128

OUTPUT FILES

130       None.
131

EXTENDED DESCRIPTION

133       None.
134

EXIT STATUS

136       If  the trap name or number is invalid, a non-zero exit status shall be
137       returned; otherwise, zero shall be returned. For both  interactive  and
138       non-interactive  shells,  invalid  signal names or numbers shall not be
139       considered a syntax error and do not cause the shell to abort.
140

CONSEQUENCES OF ERRORS

142       Default.
143
144       The following sections are informative.
145

APPLICATION USAGE

147       None.
148

EXAMPLES

150       Write out a list of all traps and actions:
151
152           trap
153
154       Set a trap so the logout utility in the directory referred  to  by  the
155       HOME environment variable executes when the shell terminates:
156
157           trap '"$HOME"/logout' EXIT
158
159       or:
160
161           trap '"$HOME"/logout' 0
162
163       Unset traps on INT, QUIT, TERM, and EXIT:
164
165           trap − INT QUIT TERM EXIT
166

RATIONALE

168       Implementations  may  permit  lowercase  signal  names as an extension.
169       Implementations may also accept the names with the SIG prefix; no known
170       historical shell does so. The trap and kill utilities in this volume of
171       POSIX.1‐2008 are now consistent in their omission of the SIG prefix for
172       signal  names.  Some  kill implementations do not allow the prefix, and
173       kill −l lists the signals without prefixes.
174
175       Trapping SIGKILL or SIGSTOP is syntactically accepted by some  histori‐
176       cal  implementations, but it has no effect. Portable POSIX applications
177       cannot attempt to trap these signals.
178
179       The output format is not historical practice. Since the output of  his‐
180       torical  trap  commands  is not portable (because numeric signal values
181       are not portable) and had to change to become so,  an  opportunity  was
182       taken  to  format  the output in a way that a shell script could use to
183       save and then later reuse a trap if it wanted.
184
185       The KornShell uses an ERR trap that is triggered whenever set −e  would
186       cause an exit. This is allowable as an extension, but was not mandated,
187       as other shells have not used it.
188
189       The text about the environment for the EXIT trap invalidates the behav‐
190       ior  of some historical versions of interactive shells which, for exam‐
191       ple, close the standard input before executing a trap on 0.  For  exam‐
192       ple,  in  some historical interactive shell sessions the following trap
193       on 0 would always print "−−":
194
195           trap 'read foo; echo "−$foo−"' 0
196
197       The command:
198
199           trap 'eval " $cmd"' 0
200
201       causes the contents of the shell variable cmd to be executed as a  com‐
202       mand when the shell exits. Using:
203
204           trap '$cmd' 0
205
206       does  not work correctly if cmd contains any special characters such as
207       quoting or redirections. Using:
208
209           trap " $cmd" 0
210
211       also works (the leading <space>  character  protects  against  unlikely
212       cases  where  cmd  is  a  decimal  integer  or begins with '−'), but it
213       expands the cmd variable when the trap command is  executed,  not  when
214       the exit action is executed.
215

FUTURE DIRECTIONS

217       None.
218

SEE ALSO

220       Section 2.14, Special Built-In Utilities
221
222       The Base Definitions volume of POSIX.1‐2008, Section 12.2, Utility Syn‐
223       tax Guidelines, <signal.h>
224
226       Portions of this text are reprinted and reproduced in  electronic  form
227       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
228       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
229       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
230       cal and Electronics Engineers,  Inc  and  The  Open  Group.   (This  is
231       POSIX.1-2008  with  the  2013  Technical Corrigendum 1 applied.) In the
232       event of any discrepancy between this version and the original IEEE and
233       The  Open Group Standard, the original IEEE and The Open Group Standard
234       is the referee document. The original Standard can be  obtained  online
235       at http://www.unix.org/online.html .
236
237       Any  typographical  or  formatting  errors that appear in this page are
238       most likely to have been introduced during the conversion of the source
239       files  to  man page format. To report such errors, see https://www.ker
240       nel.org/doc/man-pages/reporting_bugs.html .
241
242
243
244IEEE/The Open Group                  2013                             TRAP(1P)
Impressum