[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Associative array indexes not working as expected in Bash 4.0
From: |
AnMaster |
Subject: |
Associative array indexes not working as expected in Bash 4.0 |
Date: |
Tue, 24 Feb 2009 17:11:04 +0100 |
User-agent: |
Thunderbird 2.0.0.19 (X11/20090103) |
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-unknown-linux-gnu'
-DCONF_VENDOR='unknown'
-DLOCALEDIR='/home/arvid/local/shells/bash/4.0/share/locale' -DPACKAGE='bash'
-DSHELL -DHAVE_CONFIG_H -I. -I. -I./include -I./lib
-DSYS_BASHRC=/etc/bash/bashrc -DSYS_BASH_LOGOUT=/etc/bash/bash_logout
-DDEFAULT_PATH_VALUE=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
-pipe -O1 -g
uname output: Linux tux.lan 2.6.27-gentoo-r8-1 #1 Sat Jan 31 04:55:36 CET 2009
x86_64 AMD Sempron(tm) Processor 3300+ AuthenticAMD GNU/Linux
Machine Type: x86_64-unknown-linux-gnu
Bash Version: 4.0
Patch Level: 0
Release Status: release
Description:
It seems that associative array indexes are not treated in a consistent way in
bash 4.0. I was testing if associative arrays would work for what I needed (the
strings I plan to use for indexes are be untrusted and can legally contain
anything except null-bytes, thus I was testing if it was robust with "strange"
data). See "Repeat-By:" below.
So the issues are:
1. How do you escape the ] in myarray["a]a"] so that bash handles it the same
way as foo="a]a"; myarray["$foo"].
2. While myarray["a]a"] errors out when assigning, reading with
${myarray["a]a"]} doesn't error out, but doesn't return the correct result
either.
3. I'm not sure what happened in myarray['a]=test2;#a']="def". Did it eval the
single-quoted string? Seems buggy anyway.
Repeat-By:
bash-4.0$ declare -A myarray
bash-4.0$ myarray["a]a"]="abc"
bash: myarray[a]a]=abc: command not found
bash-4.0$ myarray['a]a']="abc"
bash: myarray[a]a]=abc: command not found
bash-4.0$ foo="a]a"
bash-4.0$ myarray["$foo"]="abc"
bash-4.0$ echo ${myarray["$foo"]}
abc
bash-4.0$ echo ${myarray["a]a"]}
bash-4.0$ echo ${myarray['a]a']}
bash-4.0$ echo "${myarray['a]a']}"
bash-4.0$ set | grep myarray
myarray=([a]a]="abc" )
bash-4.0$ bar='a]=test1;#a'
bash-4.0$ myarray[$bar]="123"
bash-4.0$ set | grep myarray
myarray=([a]a]="abc" [a]=test1;#a]="123" )
bash-4.0$ echo "${myarray[$bar]}"
123
bash-4.0$ echo "${myarray['a]=test2;#a']}"
bash: bad substitution: no closing `}' in ${myarray['a]=test2;#a']}
bash-4.0$ myarray['a]=test2;#a']="def"
bash-4.0$ set | grep myarray
myarray=([a]a]="abc" [a]="test2;#a]=\"def\"" [a]=test1;#a]="123" )
bash-4.0$ echo ${myarray[a]}
test2;#a]="def"
bash-4.0$ echo "${myarray['a]a']}"
test2;#a]="def"
bash-4.0$
- Associative array indexes not working as expected in Bash 4.0,
AnMaster <=