Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #11806

Re: Inconsistency in the handling of variables inside a function when using a tempenv variable

From Chet Ramey <chet.ramey@case.edu>
Newsgroups gnu.bash.bug
Subject Re: Inconsistency in the handling of variables inside a function when using a tempenv variable
Date 2015-10-30 08:47 -0400
Message-ID <mailman.1325.1446209256.7904.bug-bash@gnu.org> (permalink)
References <20151022223449.GA7965@ma.sdf.org>

Show all headers | View raw


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/

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: Inconsistency in the handling of variables inside a function when using a tempenv variable Chet Ramey <chet.ramey@case.edu> - 2015-10-30 08:47 -0400

csiph-web