bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: local variable escapes into trap


From: Chet Ramey
Subject: Re: local variable escapes into trap
Date: Tue, 29 Dec 2009 19:57:13 -0500
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.5) Gecko/20091204 Thunderbird/3.0

On 12/29/09 11:55 AM, Akim Demaille wrote:

>> I believe that bash is correct.  When the `exit' builtin is invoked, and
>> the trap is executed, the shell is still executing in the function's
>> context.  There was no `return', and running exit or a trap does not
>> implicitly call it.
> 
> I figured this out, the behavior is understandable.  But is it really
> desirable?  It's one more reason to fear shell-scripts :(  You cannot
> trust the context in which the trap will be run, in particular if you
> want to read "global" variables (and I do understand that all variables,
> including those flagged with "local", are actually global).

As I said, I believe bash is correct, and as a result, there's no reason
for me to change its behavior.  My pointing out that ksh93 behaves as
bash does was just to demonstrate that there's no standard behavior.

Posix doesn't really provide much guidance on the environment in which a
trap is run, except for saying that the environment in which the EXIT trap
is executed is identical to the environment "immediately after the last
command executed before the trap on EXIT was taken."  Is that the exit
builtin or the command before it?  Either way, I think the shell is still
executing in a function context.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]