From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29844 invoked by alias); 4 Apr 2015 19:42:01 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 20086 Received: (qmail 9738 invoked from network); 4 Apr 2015 19:41:57 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS autolearn=ham version=3.3.2 X-Injected-Via-Gmane: http://gmane.org/ To: zsh-users@zsh.org From: Thorsten Kampe Subject: Re: How to trap EXIT like in bash Date: Sat, 4 Apr 2015 21:41:31 +0200 Message-ID: References: <150404113557.ZM14788@torch.brasslantern.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: p5de5aada.dip0.t-ipconnect.de User-Agent: MicroPlanet-Gravity/3.0.4 * 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