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

Re: How to trap EXIT like in bash



On Sat, 2015-04-04 at 17:20 +0200, Thorsten Kampe wrote:
> in Bash `trap "echo trapped" EXIT` will trigger when the script 
> terminates normally and on SIGINT, SIGTERM, and SIGHUP.
> 
> In Zsh, `trap "echo trapped" EXIT` triggers only on normal exit, but 
> `trap "echo trapped" EXIT INT` will actually trigger twice on Ctrl-C.
> 
> How can I trap normal exit, Ctrl-C, SIGTERM and SIGHUP so trap 
> function will only run once?

I use this:

trap "echo trapped; exit 0" EXIT INT

That does the trick.  Both bash and zsh print trapped only once.

I have no idea why zsh is incompatible with bash there, but this
behavior has been in zsh for quite a while.

Phil.




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