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

How to trap to script EXIT instead of function EXIT?



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



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