47

I see a lot of shell scripts that do:

trap cmd 0 1 2 3 13 15 # EXIT HUP INT QUIT PIPE TERM

In every shell I have access to at the moment, all the traps other than 0 are redundant, and cmd will be executed upon receipt of a signal if the trap is simply specified:

trap cmd 0

Is the latter specification sufficient, or do some shells require the other signals to be specified?

William Pursell
  • 204,365
  • 48
  • 270
  • 300

3 Answers3

35

I think trap 0 is executed just prior to script termination in all cases, so is useful for cleanup functionality (like removing temporary files, etc). The other signals can have specialized error handling but should terminate the script (that is, call exit).

What you have described, I believe, would actually execute cmd twice. Once for the signal (for example SIGTERM) and once more on exit (trap 0).

I believe the proper way to do this is like the following (see POSIX specification for trap):

trap "rm tmpfile" 0
trap "exit 1" TERM HUP ... 

This ensures a temporary file is removed upon script completion, and lets you set custom exit statuses on signals.

NOTE: trap 0 is called whether a signal is encountered or not.

If you are not concerned with setting an exit status, trap 0 would be sufficient.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Brandon Horsley
  • 7,956
  • 1
  • 29
  • 28
  • 1
    Using `0` as a signal seems to be very poorly documented. And it does not seem to "capture" the EXIT signal. E.g. `trap "...cleanup..." 0` in my script did the cleanup on `ERR` but not on `EXIT`. – Josh M. May 26 '23 at 14:47
26

To make sure the EXIT signal handler will not be executed twice (which is almost always not what you want) it should always set to be ignored or reset within the definition of the EXIT signal handler itself.

The same goes for signals that have more than one signal handler defined for them in a program.

# reset
trap 'excode=$?; cmd; trap - EXIT; echo $excode' EXIT HUP INT QUIT PIPE TERM

# ignore
trap 'excode=$?; trap "" EXIT; cmd; echo $excode' EXIT HUP INT QUIT PIPE TERM
carlo
  • 261
  • 3
  • 2
  • This is a good idea. I don't believe it is necessary to to worry about signals with more than one trap defined, since you can only define one trap per signal. (The second trap replaces the first, rather than adding to a stack.) Do some shells allow multiple traps to be defined? – William Pursell Feb 11 '13 at 13:48
  • 3
    I was under the same confusion as @WilliamPursell until I finally noticed "ignored **or** reset". – Ryne Everett Mar 22 '15 at 23:52
10

The shell standard does not specify whether a trap on 0 is executed when an untrapped signal is received. In particular, bash and dash behave differently. Given trap cmd-list 0 with no traps set for any signals, bash will execute the cmd-list upon receipt of SIGTERM, but dash will not. Given trap cmd-list 0 2, bash executes cmd-list once upon receipt of SIGTERM, and dash executes cmd-list twice.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 4
    So the best answer is a combination of this one and Brandon Horsley's: for portability, don't assume that the `trap ... 0` will be executed on SIGTERM, and don't assume that it won't. Make a separate `trap "exit 1" SIGTERM ...` to guarantee that it will. – dubiousjim Oct 21 '12 at 14:25