[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Arrays declared global in function and initialised in same line are
From: |
Greg Wooledge |
Subject: |
Re: Arrays declared global in function and initialised in same line are not visible outside of function |
Date: |
Mon, 18 Jun 2012 14:35:41 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Mon, Jun 18, 2012 at 02:19:41PM +0100, Jacobo de Vera wrote:
> Subject: Re: Arrays declared global in function and initialised in same line
> are not visible outside of function
I can't reproduce that problem:
imadev:~$ unset a; f() { declare -ga a=(1 2); }; f; declare -p a
declare -a a='([0]="1" [1]="2")'
> Since same line is the only way to initialise readonly arrays, this
> means there is no way to have readonly global arrays created from
> function.
I also can't reproduce it with readonly:
imadev:~$ bash-4.2 -c 'unset a; f() { declare -rga a=(1 2); }; f; declare -p a'
declare -ar a='([0]="1" [1]="2")'
> See the following script:
> aa()
> {
> declare -gA arr1=(['key1']='value1')
> declare -ga arr3=(valuea valueb)
> }
>
> bb()
> {
> echo "Inside bb"
> echo "Arr1: ${arr1[key1]}"
> echo "Arr3: ${arr3[@]}"
> }
>
> aa
> bb
> Arrays arr1 and arr3, initialised on the same line as declared, are
> not visible outside the function where they are declared, whereas
> arrays modified in separate statements are.
I can't reproduce it that way either:
imadev:~$ f() { declare -ga a=(1 2); }; g() { declare -p a; }; f; g
declare -a a='([0]="1" [1]="2")'