Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #11935

Re: An array variable created by export/readonly builtins inside a function becomes a locale variable to that function unexpectedly

From konsolebox <konsolebox@gmail.com>
Newsgroups gnu.bash.bug
Subject Re: An array variable created by export/readonly builtins inside a function becomes a locale variable to that function unexpectedly
Date 2015-11-30 22:22 +0800
Message-ID <mailman.1010.1448893376.31583.bug-bash@gnu.org> (permalink)
References <70266BC6-C65A-4D95-B7C1-2F63F3C45DEF@qq.com> <20151130132909.GH27325@eeg.ccf.org>

Show all headers | View raw


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

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: An array variable created by export/readonly builtins inside a function becomes a locale variable to that function unexpectedly konsolebox <konsolebox@gmail.com> - 2015-11-30 22:22 +0800

csiph-web