10

If I issue the trap builtin twice [for the same signal], what happens? Is the second command added to the first, or does it replace the first?

trap Foo SIGINT
...
trap Bar SIGINT
...

When SIGINT happens, does Bash just run Bar, or does it run Foo as well? Or something else...?

4 Answers 4

8

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.

2
  • 2
    I read that exact paragraph, but it's not immediately clear to me that "the command arg is to be read and executed" implies that any previous command is removed. That appears to be the case, however. Nov 28, 2014 at 10:20
  • 3
    The same would apply to e.g. setting an alias... does setting an alias twice add to the existing alias, or replace it? (Of course it replaces it.) You're simply reading too much into it, just expect bash to do the simple thing :-)
    – wurtel
    Nov 28, 2014 at 10:44
7

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).

0
1

I still can't find any documentation on the matter, but from my tests it appears that the second trap specification entirely replaces the first one. (I.e., Bar will be run, but Foo will not.)

1

As already answered, the trap command replaces traps if already present. In case you do actually want both present and new to be executed, I suppose you can hack around a bit:

function overtrap {
  trap="$1"
  sig=$(echo $2 | tr [a-z] [A-Z])
  cur="$(trap -p $sig | sed -nr "s/trap -- '(.*)' $sig\$/\1/p")"
  if test ${cur:+x}; then
    trap "{ $trap; }; $cur" $sig
  else
    trap "$trap" $sig
  fi
}

Invoking something like

overtrap 'echo hi' exit
overtrap 'echo ho' exit

would print

ho
hi

(But I have no idea how portable this is.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .