tags:

views:

36

answers:

1

I am trying to create nested array's dynamically from a string that has been parsed (parameter expansion) using a for loop in bash and I am failing:

user@server:/home/user> foo=one,two,three
user@server:/home/user> for i in ${foo//,/" "}; do echo ${i}; done
one
two
three
user@server:/home/user> for i in ${foo//,/" "}; do declare -a ${i}=(var1=foo var2=foo var3=foo); done
-bash: syntax error near unexpected token `('

I can create the nested array manually and it works:

user@server:/home/user> declare -a one=(var1=foo var2=foo var3=foo)
user@server:/home/user> echo ${one[*]}
var1=foo var2=foo var3=foo

Any help would be appreciated!

+1  A: 
ecik@ecikovo / $ x=one
ecik@ecikovo / $ echo $x
one
ecik@ecikovo / $ declare -a $x=(var1=foo var2=foo)
bash: błąd składni przy nieoczekiwanym znaczniku `('
ecik@ecikovo / $ declare -a $x='(var1=foo var2=foo)'
ecik@ecikovo / $ echo ${one[0]}
var1=foo
ecik@ecikovo / $ echo ${one[1]}
var2=foo
ecik
That worked thanks!
spaghettiwestern