Re: exit trap inside ksh-style function in bash-2.05b
From:
Chet Ramey
Subject:
Re: exit trap inside ksh-style function in bash-2.05b
Date:
Thu, 25 Sep 2003 09:31:08 -0400
> (1)
>
> (function t { trap 'printf "%s" a' exit; }; t; printf "%s" b)
> => ba
>
> Per ksh man page:
>
> A trap on EXIT set inside a [ksh-style] function is executed in
> the environment of the caller after the function completes.
>
> Also Bolsky and Korn, page 162.
>
> Since bash adopted the ksh-style function, it should follow its
> documentation. I see no good reason to be different.
Bash has not `adopted the ksh-style function'. Bash functions conform
to POSIX.2, regardless of the syntax used to declare them.
POSIX.2 requires that shell functions share the calling shell's
environment, which includes traps.
> As a supporting evidence, "zsh version 3.0" that I happen to have
> (I am not a zsh user) behaves the same as the correct implementation
> of ksh. That is, (1) produces "ab" and $z in (2) is "ab".
How is this `supporting evidence'? Bash isn't ksh, and it's not zsh,
either.
Now, that said, bash-3.0 includes a `RETURN' trap, which is executed when
a shell function or sourced script returns. It was implemented as part
of the bash debugger support.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet )
Live...Laugh...Love
Chet Ramey, ITS, CWRU chet@po.cwru.edu http://tiswww.tis.cwru.edu/~chet/