[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: 'local -x VARIABLE' does not clear variable for subprocesses
From: |
Chet Ramey |
Subject: |
Re: 'local -x VARIABLE' does not clear variable for subprocesses |
Date: |
Wed, 07 May 2014 10:04:11 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 |
On 5/7/14, 2:10 AM, Dan Douglas wrote:
> On Monday, May 05, 2014 11:18:50 AM Chet Ramey wrote:
>> The idea behind the bash-4.3 behavior is that the placeholder local
>> variable isn't set, and doesn't really exist. It doesn't shadow a
>> global variable with the same name until it gets a value. The bash-4.2
>> behavior was inconsistent: variables with attributes but without values
>> weren't quite set (value == 0x0, but still visible) and weren't quite
>> unset. I tightened up the some of the consistency starting with bash-4.3
>> beta. I'm sure there are still inconsistencies there (and, in fact, I
>> found one while looking at this).
>
> By "doesn't shadow" you mean that it _does_ hide the global right? Localizing
> a variable should cover up globals and variables in parent scopes even if
> they
> aren't given a value. That seems true in bash 4.3 and nearly every shell
> (except dash).
I mean that a local variable that has not been given a value, and is
technically unset, should not shadow a global variable with the same
name. Run the following script, after accounting for the local/typeset
differences between shells. The results are less conclusive than you think.
export VAR1=abc VAR2=abc
f()
{
local VAR1; local VAR2
echo local: ${VAR1-unset1} ${VAR2-unset2}
printenv VAR1 ; printenv VAR2
}
f
Shells that shadow print `local: unset1 unset2'. Shells that don't shadow
print `local: abc abc'. Everyone except bash-4.2 seems to print
`abc\nabc' with the printenv calls.
Shadow: bash-4.3, mksh, posh
Doesn't shadow: ksh93, dash, freebsd sh
zsh is off doing its own thing and just prints `local:'. I presume that
means it silently assigns a null value.
> If you want to set an attribute without localizing the variable then you've
> always been able to use "export" or "readonly" in place of "local -x".
Sure, but the OP wants to do exactly the opposite, and more.
> I also wouldn't want this example to change so that ref always appears set.
> The localized x in g should hide f's x even with no value.
namerefs are their own special beasts.
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/