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 [email protected] http://cnswww.cns.cwru.edu/~chet/