Discussion:
[Help-bash] Inconsistent results of `trap '' EXIT`
Peng Yu
2018-02-20 15:02:43 UTC
Permalink
The following code shows that `trap '' EXIT` has a different effect
that depends on whether it has been set before. It is better to make
`trap '' EXIT` reset the signal instead of using `trap - EXIT`?

$ trap -p EXIT
$ trap 'echo abc' EXIT
$ trap -p EXIT
trap -- 'echo abc' EXIT
$ trap '' EXIT
$ trap -p EXIT
trap -- '' EXIT
--
Regards,
Peng
Eduardo A. Bustamante López
2018-02-20 15:14:58 UTC
Permalink
Post by Peng Yu
The following code shows that `trap '' EXIT` has a different effect
that depends on whether it has been set before. It is better to make
`trap '' EXIT` reset the signal instead of using `trap - EXIT`?
$ trap -p EXIT
$ trap 'echo abc' EXIT
$ trap -p EXIT
trap -- 'echo abc' EXIT
$ trap '' EXIT
$ trap -p EXIT
trap -- '' EXIT
Where is the inconsistency? I only see `trap '' EXIT` being executed once.

Perhaps you are confusing it with `trap - EXIT`?

***@ubuntu:~$ help trap | grep 'ARG is a' -A3
ARG is a command to be read and executed when the shell receives the
signal(s) SIGNAL_SPEC. If ARG is absent (and a single SIGNAL_SPEC
is supplied) or `-', each specified signal is reset to its original
value. If ARG is the null string each SIGNAL_SPEC is ignored by the
shell and by the commands it invokes.

The former *ignores* the signal, the latter *resets* it to the original value.

i.e.

***@ubuntu:~$ trap -p EXIT
***@ubuntu:~$ trap 'echo abc' EXIT
***@ubuntu:~$ trap -p EXIT
trap -- 'echo abc' EXIT
***@ubuntu:~$ trap - EXIT
***@ubuntu:~$ trap -p EXIT

Loading...