[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Arrays declared global in function and initialised in same line are not
From: |
Jacobo de Vera |
Subject: |
Arrays declared global in function and initialised in same line are not visible outside of function |
Date: |
Mon, 18 Jun 2012 14:19:41 +0100 |
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu'
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash'
-DSHELL -DHAVE_CONFIG_H -I. -I../bash -I../bash/include
-I../bash/lib -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector
--param=ssp-buffer-size=4 -Wformat -Wformat-security
-Werror=format-security -Wall
uname output: Linux kalgan 3.2.0-24-generic #39-Ubuntu SMP Mon May 21
16:52:17 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu
Bash Version: 4.2
Patch Level: 24
Release Status: release
Description:
See the following script:
aa()
{
declare -gA arr1=(['key1']='value1')
declare -gA arr2
declare -ga arr3=(valuea valueb)
declare -ga arr4
arr4[1]=valuea
arr4[2]=valueb
arr2[key2]=value2
echo "Inside aa"
echo "Arr1: ${arr1[key1]}"
echo "Arr2: ${arr2[key2]}"
echo "Arr3: ${arr3[@]}"
echo "Arr4: ${arr4[@]}"
}
bb()
{
echo "Inside bb"
echo "Arr1: ${arr1[key1]}"
echo "Arr2: ${arr2[key2]}"
echo "Arr3: ${arr3[@]}"
echo "Arr4: ${arr4[@]}"
}
aa
bb
echo "Global scope"
echo "Arr1: ${arr1[key1]}"
echo "Arr2: ${arr2[key2]}"
echo "Arr3: ${arr3[@]}"
echo "Arr4: ${arr4[@]}"
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.
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.
Repeat-By:
See function in description
- Arrays declared global in function and initialised in same line are not visible outside of function,
Jacobo de Vera <=