From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2096 invoked by alias); 4 Apr 2015 18:36:15 -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: 20084 Received: (qmail 1610 invoked from network); 4 Apr 2015 18:36:04 -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 autolearn=ham version=3.3.2 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:message-id:date:in-reply-to:comments :references:to:subject:mime-version:content-type; bh=T9HCPh1FhkGQTTUMNuihCkjEX97XAqp1Y2Ppp+Jkz5w=; b=XyBhw0yx144AU3GL7v+HEEUfEuxLz5jkOPjyuiPpR8JPQfKp1Sw1cScpik0G4Z/gix cw2Dhi0nrM412kjNhTIpHCl9AiMDL7y9JXC/3qyxjqpL7H8isvOZUGuDCZAkJHjWvpfF y2tnKXtIRrcQ6Qp+qU9YWb9guCXPo1gJBV1Qrqo43uWcwdRe3uqNNDwfWTO5gaz/E/o0 c8BJbieKuWwj0NAXb8A+Xgiyv2yzitqbGd92t/yPVRbrE9SIyliXPrcItVRwNDaNae45 UuvujDhav5pX+aE7EMQjbQlDKvFUL8A2B6ZhG18jhxcpYK9YQMioJwGzFdBFWAfGFv07 509Q== X-Gm-Message-State: ALoCoQm2rOoQujfzYExyvoIrEodivpjO+KJ9xed+34AO5f70bdhf3H1QWAlgH4QdzY9ozyHNu2gU X-Received: by 10.182.87.74 with SMTP id v10mr9521748obz.50.1428172560764; Sat, 04 Apr 2015 11:36:00 -0700 (PDT) From: Bart Schaefer Message-Id: <150404113557.ZM14788@torch.brasslantern.com> Date: Sat, 4 Apr 2015 11:35:57 -0700 In-Reply-To: Comments: In reply to Thorsten Kampe "How to trap EXIT like in bash" (Apr 4, 5:20pm) References: X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Re: How to trap EXIT like in bash MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii 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 Curiously in an interactive shell, the following prints "trapped" exactly one time, even though my first answer above also works interactively: trap "echo trapped" EXIT trap exit HUP INT TERM However, that does not work in a script. I'm not sure why interactive matters here.