Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Can i use a variable inside () syntax

#!/bin/bash
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----------
share|improve this question

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.

add comment

2 Answers 2

up vote 3 down vote accepted

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----------

Explanation

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.

share|improve this answer
add comment

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.

share|improve this answer
    
This does not answer the question, why the given syntax does not work. –  michas Oct 12 '13 at 13:40
add comment

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