On Mon, Nov 30, 2015 at 9:29 PM, Greg Wooledge <wool...@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