I am trying to do something like below:
for a in apple orange grape; do
${!a}="blah"
done
echo $apple
blah
Possible?
Use declare.
for a in apple orange grape; do
declare "$a=blah"
done
declare $a='blah' - there's no need to or point in double-quoting $a, because declare will not apply expansions to the left side (and anything that truly needs quoting wouldn't work as a variable name anyway).declare: printf -v $a "%s" "blah"I wonder if you might want to use associative arrays instead:
declare -A myarray
for a in apple orange grape; do
myarray[$a]="blah"
done
echo ${myarray[apple]}
Note associative arrays require bash version 4.0 or greater.