Bash: issuing trap twice

The command is replaced.

The manpage states:

 trap [-lp] [[arg] sigspec ...]
        The  command  arg  is  to  be  read  and executed when the shell
        receives signal(s) sigspec.  If arg is absent (and  there  is  a
        single  sigspec)  or  -,  each  specified signal is reset to its
        original disposition (the value it  had  upon  entrance  to  the
        shell).   If arg is the null string the signal specified by each
        sigspec is ignored by the shell and by the commands it  invokes.
        If  arg  is  not present and -p has been supplied, then the trap
        commands associated with each  sigspec  are  displayed.   If  no
        arguments  are  supplied or if only -p is given, trap prints the
        list of commands associated with each  signal.   The  -l  option
        causes  the shell to print a list of signal names and their cor‐
        responding numbers.   Each  sigspec  is  either  a  signal  name
        defined  in  <signal.h>,  or  a signal number.  Signal names are
        case insensitive and the SIG prefix is optional.

It states the command arg is to be read and executed ... period. It would otherwise not be possible to reset the signal handling if the arg was always added to the list.


From the manual:

trap [-lp] [arg] [sigspec …]

The commands in arg are to be read and executed when the shell receives signal sigspec.

The description doesn't say anything about adding to an existing list of commands. It goes on to specify non-incremental effects when arg is empty or the string -. While the text might not explicitly say that the commands are not added to a list, it never mentions any such list, or any means to remove an item from said list. So interpreting this text in a way that would imply that successive trap commands add arg to a list is rather far-fetched.

You can make sure by checking another shell's manual. If bash deviated from the usual behavior, the manual would clearly say so. The POSIX standard is unambiguous on the matter:

The action of trap shall override a previous action (either default action or one explicitly set).

Tags:

Shell

Bash

Trap