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?
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