0

I'm having what seems like a pretty simple problem that I can't seem to fix, and the only help I'm getting from bash is that it's a bad substitution. Any help?

#!/bin/bash
lang=Python
frameworks=('Python=(Django Flask Pyramid)' 'Ruby=(Rails Cuba)')
for i in "${frameworks[@]}";do eval $i;done
echo "Python ${#$lang[@]} ${$lang[@]}"
echo "Ruby ${#Ruby[@]} ${Ruby[@]}"

Line 5 above is what throws the error, but line 6 works perfectly, which should do the same thing?

Output:

>>>line 5: Python ${#$lang[@]} ${$lang[@]}: bad substitution
>>>Ruby 2 Rails Cuba

2 Answers2

2

You can't use $lang in this way (obviously). The bash array construct ${name[@]} is a little bit sacred. If you want to deference on $lang this way, you'll need another eval.

Try this instead:

eval "echo \"Python  \${#$lang[@]} \${$lang[@]}\""
ghoti
  • 45,319
  • 8
  • 65
  • 104
1

Use indirection. Assign what you want to expand to a variable, in this case Python[@], and then use ${!thatvariable}:

mything="$lang[@]"
echo "${!mything}"
that other guy
  • 116,971
  • 11
  • 170
  • 194