1trap [ arg ] [ sig ... ] 2 arg is a series of commands (usually quoted to protect it from 3 immediate evaluation by the shell) to be read and executed when 4 the shell receives any of the signals specified by one or more 5 sig args. Each sig can be given as a number, or as the name of 6 a signal either with or without the string SIG in front (e.g. 1, 7 HUP, and SIGHUP are all the same signal). 8 9 If arg is `-', then the specified signals are reset to their 10 defaults, or, if no sig args are present, all traps are reset. 11 12 If arg is an empty string, then the specified signals are 13 ignored by the shell (and by the commands it invokes). 14 15 If arg is omitted but one or more sig args are provided (i.e. 16 the first argument is a valid signal number or name), the effect 17 is the same as if arg had been specified as `-'. 18 19 The trap command with no arguments prints a list of commands 20 associated with each signal. 21 22 If sig is ZERR then arg will be executed after each command with 23 a nonzero exit status. ERR is an alias for ZERR on systems that 24 have no SIGERR signal (this is the usual case). 25 26 If sig is DEBUG then arg will be executed before each command if 27 the option DEBUG BEFORE CMD is set (as it is by default), else 28 - - 29 after each command. Here, a `command' is what is described as a 30 `sublist' in the shell grammar, see the section SIMPLE COMMANDS 31 & PIPELINES in zshmisc(1). If DEBUG BEFORE CMD is set various 32 - - 33 additional features are available. First, it is possible to 34 skip the next command by setting the option ERR EXIT; see the 35 - 36 description of the ERR EXIT option in zshoptions(1). Also, the 37 - 38 shell parameter ZSH DEBUG CMD is set to the string corresponding 39 - - 40 to the command to be executed following the trap. Note that 41 this string is reconstructed from the internal format and may 42 not be formatted the same way as the original text. The parame- 43 ter is unset after the trap is executed. 44 45 If sig is 0 or EXIT and the trap statement is executed inside 46 the body of a function, then the command arg is executed after 47 the function completes. The value of $? at the start of execu- 48 tion is the exit status of the shell or the return status of the 49 function exiting. If sig is 0 or EXIT and the trap statement is 50 not executed inside the body of a function, then the command arg 51 is executed when the shell terminates; the trap runs before any 52 zshexit hook functions. 53 54 ZERR, DEBUG, and EXIT traps are not executed inside other traps. 55 ZERR and DEBUG traps are kept within subshells, while other 56 traps are reset. 57 58 Note that traps defined with the trap builtin are slightly dif- 59 ferent from those defined as `TRAPNAL () { ... }', as the latter 60 have their own function environment (line numbers, local vari- 61 ables, etc.) while the former use the environment of the command 62 in which they were called. For example, 63 64 trap 'print $LINENO' DEBUG 65 66 will print the line number of a command executed after it has 67 run, while 68 69 TRAPDEBUG() { print $LINENO; } 70 71 will always print the number zero. 72 73 Alternative signal names are allowed as described under kill 74 above. Defining a trap under either name causes any trap under 75 an alternative name to be removed. However, it is recommended 76 that for consistency users stick exclusively to one name or 77 another. 78