bug-bash
[Top][All Lists]
Advanced

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

Re: An array variable created by export/readonly builtins inside a funct


From: konsolebox
Subject: Re: An array variable created by export/readonly builtins inside a function becomes a locale variable to that function unexpectedly
Date: Mon, 30 Nov 2015 22:22:52 +0800

On Mon, Nov 30, 2015 at 9:29 PM, Greg Wooledge <wooledg@eeg.ccf.org> wrote:
> On Sat, Nov 28, 2015 at 11:18:24AM +0800, ziyunfei wrote:
>> $ bash -c 'foo() { readonly a=(1);echo a=$a; }; foo; echo a=$a' # a becomes 
>> a local variable
>> a=1
>> a=
>
> "readonly" is a synonym for "declare -r", and declare (without the -g
> option) always marks variables as local when used in a function.

That's also how I used to think about it, but I'm not sure when it has
been official.  No part of the documentation seems to tell that
`readonly` acts similar to `declare`, `typeset` and `local`.

Also a variable seems to be only made local when it is defined as an
array variable.

$ bash -c 'a=0; foo() { readonly a=(1); echo a=$a; }; foo; a=2; echo a=$a'
a=1
a=2
$ bash -c 'a=0; foo() { readonly a=1; echo a=$a; }; foo; a=2; echo a=$a'
a=1
bash: a: readonly variable



reply via email to

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