Sunday, 15 August 2010

shell - Bash: Can the values of an associative array be arrays? What's its syntax? -



shell - Bash: Can the values of an associative array be arrays? What's its syntax? -

i have associative array stored in bash file, , i'd values of said associative array arrays, don't know if that's possible:

let's have file /tmp/conf.bashcontaining:

declare -a associative=( ["foo"]=( "bar" "baz" ) ) when tried load (using source /tmp/conf.bash) get:

borrajax@wharrgarbl:~# source /tmp/conf.bash bash: /tmp/conf.bash: line 2: syntax error near unexpected token `(' bash: /tmp/conf.bash: line 2: ` ["foo"]=( "bar" "baz" )' bash: /tmp/conf.bash: line 3: syntax error near unexpected token `)' bash: /tmp/conf.bash: line 3: ` )'

but, if create file:

declare -a associative=( ["foo"]="bar baz" )

it works correctly:

borrajax@wharrgarbl:~# source /tmp/conf.bash borrajax@hwharrgarbl:~# key in "${!associative[@]}"; \ echo "key: $key; values: ${associative[$key]}"; \ done key: foo; values: bar baz

is there way of specifying values arrays in bash script?

one possibility set values of associative array comma-separated strings , split them array, thought maybe there's way of formatting bash file step unnecessary.

i'm using bash 4.2.28

thank in advance.

no. values in array (whether indexed or associative) can strings. helps think of bash arrays not info structures, sec level of quoting like

args=("foo bar" "baz") mycommand "${args[@]}"

passes 2 arguments, not three, mycommand. in scenario, nesting not required, since command can take strings, not arrays, arguments.

bash shell arrays

No comments:

Post a Comment