|
From: | Chris F.A. Johnson |
Subject: | Re: Local variables overriding global constants |
Date: | Wed, 3 Apr 2013 04:50:05 -0400 (EDT) |
User-agent: | Alpine 2.00 (LMD 1167 2008-08-23) |
On Wed, 3 Apr 2013, Pierre Gaston wrote:
On Wed, Apr 3, 2013 at 11:33 AM, Chris F.A. Johnson <address@hidden>wrote:On Wed, 3 Apr 2013, Pierre Gaston wrote: On Wed, Apr 3, 2013 at 11:03 AM, Chris Down <address@hidden> wrote:On 2013-04-03 11:00, Nikolai Kondrashov wrote:It doesn't work because you are trying to redefine an existingreadonly variable.Yes, but I'm explicitly redefining it locally, only for this function. And this works for variables previously defined in the calling function.You're not redefining it locally, you are unsuccessfully trying to override a global.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 variableif 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(). -- Chris F.A. Johnson, <http://cfajohnson.com/> Author: Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress) Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
[Prev in Thread] | Current Thread | [Next in Thread] |