Re: EXIT trap does not fire

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I can confirm this behavior in dash
0.5.11+git20210903+057cd650a4ed-3build1 (on Ubuntu).

Signal terminations are not caught by EXIT. It only catches normal
exits. Unfortunately, the EXIT condition is not well-defined by POSIX,
so it's left to interpretation. In the EXAMPLES section of `trap`,
it's hinted that EXIT should be able to handle all terminations (thus,
acting like a shortcut):

> Set a trap so the logout utility in the directory referred to by the HOME environment variable executes when the shell terminates:
>   trap '"$HOME"/logout' EXIT

In dash:

#!/bin/dash
> /tmp/file
trap 'rm -f /tmp/file' EXIT
read -r input

the action is never executed if the script is sent any terminating
signal (HUP, ABRT, INT, TERM, you name it).

In bash, however, it seems to work fine. The action is executed and
the exit status is preserved.

I'm not sure what The Open Group is planning to do and whether or not
we're getting any clarification regarding this. Until then, I think
it's reasonable to interpret or even expect that the EXIT condition
concerns all kinds of terminations, not just explicit program exits.

To conclude, I just wanna say that

     trap 'cleanup' EXIT

is shorter/cleaner/better/saner than

     trap 'cleanup' HUP ABRT INT QUIT TERM KILL PIPE ...



[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux