Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!usenet.stanford.edu!not-for-mail From: Chet Ramey Newsgroups: gnu.bash.bug 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 Lines: 38 Approved: bug-bash@gnu.org Message-ID: References: <20151022223449.GA7965@ma.sdf.org> Reply-To: chet.ramey@case.edu NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: usenet.stanford.edu 1446209256 31794 208.118.235.17 (30 Oct 2015 12:47:36 GMT) X-Complaints-To: action@cs.stanford.edu Cc: chet.ramey@case.edu To: bug-bash@gnu.org Envelope-to: bug-bash@gnu.org X-Enigmail-Draft-Status: N1110 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: <20151022223449.GA7965@ma.sdf.org> X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020205.563366DF.00CC, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2015-08-12 04:07:17, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 966397cb5ea8c4cf5e4a892162148cab X-Junkmail-Whitelist: YES (by domain whitelist at mpv4-2015.case.edu) X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020206.563366E1.024F,ss=1,re=0.000,fgs=0, ip=0.0.0.0, so=2015-08-12 04:07:17, dmn=2011-05-27 18:58:46 X-Mirapoint-Loop-Id: a3222b657188562987b251083d655cd4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 129.22.103.195 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:11806 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/