Can i use a variable inside () syntax

declare -A c
declare -A b
a="[a]=0 [b]=1 [c]=2 [d]=3"
b=($a) # or b=($(echo "$a"))
echo "***********${b[@]}*********"  #********************

c=([a]=0 [b]=1 [c]=2 [d]=3)
echo "---------${c[@]}----------"  #---------0 1 2 3----------

closed as unclear what you're asking by terdon, slm, Anthon, manatwork, jasonwryan Oct 12 '13 at 18:47

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


It's currently not possible (to my knowledge) to be able to take strings and create associative arrays with them in Bash. Therefore you must "evaluate" them prior using the eval statement.

When I run your example I get the following using Bash version 4.1.7:

$ ./arr.bash 
./arr.bash: line 5: b: [a]=0: must use subscript when assigning associative array
./arr.bash: line 5: b: [b]=1: must use subscript when assigning associative array
./arr.bash: line 5: b: [c]=2: must use subscript when assigning associative array
./arr.bash: line 5: b: [d]=3: must use subscript when assigning associative array

Changing the line to this works:

eval "b=($a)" # or b=($(echo "$a"))

Re-running you now get the expected results:

$ ./arr.bash 
***********0 1 2 3*********
---------0 1 2 3----------


I found this SO Q&A titled: Bash: How to assign an associative array to another variable name (e.g. rename the variable)?, which illustrates a method to do this using declare but it goes to show how unreadable this method actually is, and should probably not be used.

# declare associative array
declare -A assoc_array=(["key1"]="value1" ["key2"]="value2")
# convert associative array to string
assoc_array_string=$(declare -p assoc_array)
# create new associative array from string
eval "declare -A new_assoc_array="${assoc_array_string#*=}
# show array definition
declare -p new_assoc_array

This method shows the $assoc_array being converted to a string and then converted back to an associative array, $new_assoc_array. But even this method can't escape the need to use eval. This is a difficult code snippet to follow and goes to show why it really should be avoided.

  • 1
    Why eval? You can run declare with parameterized contents with no eval preceding. – Charles Duffy Mar 30 '17 at 15:48
  • 1
    s='["key1"]="value1" ["key2"]="value2"'; declare -A assoc_array="($s)"; echo "Value of key1 is ${assoc_array[key1]}" <- no eval needed. – Charles Duffy Mar 30 '17 at 15:49
  • @CharlesDuffy, thanks. Simply surrounding the part to the right of the equals sign with double quotes worked. – jrahhali Nov 30 '18 at 15:07

You need

eval "b=($a)"

eval will evaluate its argument as a shell command after performing parameter expansion. Use with care, though. Make sure that $a is not set by user input as this may lead to all sorts of trouble.

  • This does not answer the question, why the given syntax does not work. – michas Oct 12 '13 at 13:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.