1trap(1) User Commands trap(1)
2
3
4
6 trap, onintr - shell built-in functions to respond to (hardware) sig‐
7 nals
8
10 sh
11 trap [argument n [n2]...]
12
13
14 csh
15 onintr [-| label]
16
17
18 ksh
19 *trap [arg sig [sig2...]]
20
21
22 ksh93
23 +trap [-p] [action condition...]
24
25
27 sh
28 The trap command argument is to be read and executed when the shell
29 receives numeric or symbolic signal(s) (n). (Note: argument is scanned
30 once when the trap is set and once when the trap is taken.) Trap com‐
31 mands are executed in order of signal number or corresponding symbolic
32 names. Any attempt to set a trap on a signal that was ignored on entry
33 to the current shell is ineffective. An attempt to trap on signal 11
34 (memory fault) produces an error. If argument is absent all trap(s) n
35 are reset to their original values. If argument is the null string this
36 signal is ignored by the shell and by the commands it invokes. If n is
37 0 the command argument is executed on exit from the shell. The trap
38 command with no arguments prints a list of commands associated with
39 each signal number.
40
41 csh
42 onintr controls the action of the shell on interrupts. With no argu‐
43 ments, onintr restores the default action of the shell on interrupts.
44 (The shell terminates shell scripts and returns to the terminal command
45 input level). With the − argument, the shell ignores all interrupts.
46 With a label argument, the shell executes a goto label when an inter‐
47 rupt is received or a child process terminates because it was inter‐
48 rupted.
49
50 ksh
51 trap uses arg as a command to be read and executed when the shell
52 receives signal(s) sig. arg is scanned once when the trap is set and
53 once when the trap is taken. Each sig can be specified as a number or
54 as the name of the signal. trap commands are executed in order of sig‐
55 nal number. Any attempt to set a trap on a signal that was ignored on
56 entry to the current shell is ineffective. If arg is omitted or is −,
57 then the trap(s) for each sig are reset to their original values. If
58 arg is the null (the empty string, for example, "") string then this
59 signal is ignored by the shell and by the commands it invokes. If sig
60 is ERR then arg are executed whenever a command has a non-zero exit
61 status. If sig is DEBUG then arg are executed after each command. If
62 sig is 0 or EXIT for a trap set outside any function then the command
63 arg is executed on exit from the shell. The trap command with no argu‐
64 ments prints a list of commands associated with each signal number.
65
66
67 On this manual page, ksh(1) commands that are preceded by one or two *
68 (asterisks) are treated specially in the following ways:
69
70 1. Variable assignment lists preceding the command remain in
71 effect when the command completes.
72
73 2. I/O redirections are processed after variable assignments.
74
75 3. Errors cause a script that contains them to abort.
76
77 4. Words, following a command preceded by ** that are in the
78 format of a variable assignment, are expanded with the same
79 rules as a variable assignment. This means that tilde sub‐
80 stitution is performed after the = sign and word splitting
81 and file name generation are not performed.
82
83 ksh93
84 trap is a special built-in that defines actions to be taken when condi‐
85 tions such as receiving a signal occur. trap can also be used to dis‐
86 play the current trap settings on standard output.
87
88
89 If action is -, trap resets each condition to the default value. If
90 action is an empty string, the shell ignores each of the conditions if
91 they arise. Otherwise, the argument action is read and executed by the
92 shell as if it were processed by eval when one of the corresponding
93 conditions arise. The action of the trap overrides any previous action
94 associated with each specified condition. The value of $? is not
95 altered by the trap execution.
96
97
98 condition can be the name or number of a signal, or one of the follow‐
99 ing:
100
101 EXIT Execute this trap when the shell exits. If defined within a
102 function with the function reserved word, executes the trap in
103 the caller's environment when the function returns. The trap
104 action is restored to the value it had when it called the
105 function.
106
107
108 0 Same as EXIT.
109
110
111 DEBUG Execute before each simple command is executed but after the
112 arguments are expanded.
113
114
115 ERR Execute whenever set -e would cause the shell to exit.
116
117
118 KEYBD Execute when a key is entered from a terminal device.
119
120
121
122 Signal names are case insensitive and the sig prefix is optional. Sig‐
123 nals that were ignored on entry to a non-interactive shell cannot
124 trapped or reset although doing so does not report an error. The use of
125 signal numbers other than 1, 2, 3, 6, 9, 14, and 15 are not portable.
126
127
128 Although trap is a special built-in, specifying a condition that the
129 shell does not know about causes trap to exit with a non-zero exit sta‐
130 tus, but does not terminate the invoking shell.
131
132
133 If no action or conditions are specified then all the current trap set‐
134 tings are written to standard output.
135
136
137 The following options are supported by the trap built-in command in
138 ksh93:
139
140 -p Causes the current traps to be output in a format that can be
141 processed as input to the shell to recreate the current traps.
142
143
144
145 The trap built-in in ksh93 exits with one of the following values:
146
147 0 Successful completion.
148
149
150 >0 An error occurred.
151
152
153
154 On this manual page, ksh93(1) commands that are preceded by one or two
155 + (plus signs) are treated specially in the following ways:
156
157 1. Variable assignment lists preceding the command remain in
158 effect when the command completes.
159
160 2. I/O redirections are processed after variable assignments.
161
162 3. Errors cause a script that contains them to abort.
163
164 4. They are not valid function names.
165
166 5. Words, following a command preceded by ++ that are in the
167 format of a variable assignment, are expanded with the same
168 rules as a variable assignment. This means that tilde sub‐
169 stitution is performed after the = sign and word splitting
170 and file name generation are not performed.
171
173 See attributes(5) for descriptions of the following attributes:
174
175
176
177
178 ┌─────────────────────────────┬─────────────────────────────┐
179 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
180 ├─────────────────────────────┼─────────────────────────────┤
181 │Availability │SUNWcsu │
182 └─────────────────────────────┴─────────────────────────────┘
183
185 csh(1), eval(1), exit(1), ksh(1), ksh93(1), sh(1), attributes(5)
186
187
188
189SunOS 5.11 20 Nov 2007 trap(1)