On Wed, Apr 3, 2013 at 11:33 AM, Chris F.A. Johnson <chris@cfajohnson.com>wrote:
On Wed, 3 Apr 2013, Pierre Gaston wrote:
On Wed, Apr 3, 2013 at 11:03 AM, Chris Down <chris@chrisdown.name> wrote:
On 2013-04-03 11:00, Nikolai Kondrashov wrote:
It doesn't work because you are trying to redefine an existing
readonly 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 variable
if v doesn't exist in "a" why does it complain that it's readonly?