bug-bash
[Top][All Lists]
Advanced

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

Re: Inconsistency in the handling of variables inside a function when us


From: Chet Ramey
Subject: Re: Inconsistency in the handling of variables inside a function when using a tempenv variable
Date: Fri, 30 Oct 2015 08:47:25 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Thunderbird/38.3.0

On 10/22/15 6:34 PM, Eduardo A. Bustamante López wrote:

> dualbus@hp ~ % bash -xc 'f() { x=3; local x; echo $x; }; f; x=4 f'            
>                          
> + f
> + x=3
> + local x
> + echo
> 
> + x=4
> + f
> + x=3
> + local x
> + echo 3
> 3
> 
> I'm putting it here so I don't forget about it.
> 
> Bash is the only one that handles this: tmpenv - global - local variable chain
> in that way.

In the first case, the x=3 sets a global variable; the local x creates a
local variable, and the echo $x finds the local.

Bash unifies the temporary environment and local variable environment for
commands, like shell functions, for which the temporary environment must
persist throughout multiple commands (source/., eval, and mapfile are
others).  There's really no difference in how they need to be handled.
The x=4 creates a temporary environment variable, which the x=3 modifies.
Since they're unified, the local declaration inherits the value of the
tempenv variable.  Bash has done this for a very long time -- at least 20
years -- so there is backwards compatibility to consider.

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]