bug-bash
[Top][All Lists]
Advanced

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

Re: Local variables overriding global constants


From: Pierre Gaston
Subject: Re: Local variables overriding global constants
Date: Wed, 3 Apr 2013 11:50:13 +0300

On Wed, Apr 3, 2013 at 11:50 AM, Chris F.A. Johnson <chris@cfajohnson.com>wrote
>
>   Still Nikolai has a point.
>>>>>
>>>>
>  It's not clear why readonly variable can be overridden when the
>>>> variable is declared readonly in the scope of an englobing
>>>> function but not if it is declared readonly in the global scope.
>>>>
>>>
>>>    If it's declared readonly in a function, the variable doesn't exist
>>>    outside of that function, so it's not readonly there.
>>>
>>
>> I think you missed the point that "a" is called inside "b".
>> See the example below
>>
>>   $ bash -c 'a() {  v=2;echo "$v"; }; b () { declare -r v=1; a; echo "$v";
>>>
>>>> };
>>>> b'
>>>> bash: v: readonly variable
>>>>
>>>
>> if v doesn't exist in "a" why does it complain that it's readonly?
>>
>
>    It *does* exist inside a() if a() is a child of b().


.... yes, and it's readonly, but you can redeclare it locally, something
you can't do with a readonly global variable.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]