Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: How to trap EXIT like in bash



* Bart Schaefer (Sat, 4 Apr 2015 11:35:57 -0700)
> 
> On Apr 4,  5:20pm, Thorsten Kampe wrote:
> }
> } In Zsh, `trap "echo trapped" EXIT` triggers only on normal exit, but 
> } `trap "echo trapped" EXIT INT` will actually trigger twice on Ctrl-C.
> 
> Hmm.  This seems to be a side-effect of the rule that the EXIT trap does
> not run from inside other traps.  The default response to INT in a script
> is to behave as if 'trap "exit 130" INT' so the EXIT trap is not run.  In
> your second (executes twice) example, the script doesn't exit until after
> the INT trap has completed.
> 
> } How can I trap normal exit, Ctrl-C, SIGTERM and SIGHUP so trap 
> } function will only run once?
> 
> Just add an explicit "exit" to the trap itself:
> 
>     trap "echo trapped; exit" EXIT HUP INT TERM
> 
> This also works:
> 
>     zshexit() { echo trapped }
>     trap exit HUP INT TERM

As stated to Philippe: this works in regard to exit and INT, but not 
for HUP and TERM. The script ignores any `kill PID` request.

Tested on Cygwin and Ubuntu 14.10.

Thorsten



Messages sorted by: Reverse Date, Date, Thread, Author