zsh-users
 help / color / mirror / code / Atom feed
From: Peng Yu <pengyu.ut@gmail.com>
To: zsh-users <zsh-users@zsh.org>
Subject: How to trap to script EXIT instead of function EXIT?
Date: Sun, 2 Dec 2018 22:09:37 -0600	[thread overview]
Message-ID: <CABrM6wm-dKVroNPXA3-ogthrW4ErbA603cPcbNyxNvb+Dz2ppQ@mail.gmail.com> (raw)

Hi,

The following example shows that `trap` inside a function is trapped
to the function. This behavior is different from bash. Is there a way
to make it the same as bash?

$ cat main.sh
#!/usr/bin/env zsh
# vim: set noexpandtab tabstop=2:

set -v
function f {
	trap "echo 'Hello World!'" EXIT
	trap
}
f
trap
$ ./main.sh
function f {
	trap "echo 'Hello World!'" EXIT
	trap
}
f
trap -- 'echo '\''Hello World!'\' EXIT
Hello World!
trap

$ cat ./main.bash
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
function f {
	trap "echo 'Hello World!'" EXIT
	trap
}
f
trap
$  ./main.bash
function f {
	trap "echo 'Hello World!'" EXIT
	trap
}
f
trap -- 'echo '\''Hello World!'\''' EXIT
trap
trap -- 'echo '\''Hello World!'\''' EXIT
echo 'Hello World!'
Hello World!

-- 
Regards,
Peng

             reply	other threads:[~2018-12-03  4:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-03  4:09 Peng Yu [this message]
2018-12-03  7:41 ` Bart Schaefer
2018-12-03 14:05   ` Peng Yu
2018-12-03 14:59     ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CABrM6wm-dKVroNPXA3-ogthrW4ErbA603cPcbNyxNvb+Dz2ppQ@mail.gmail.com \
    --to=pengyu.ut@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).