How to create variable variables in bash
Keywords: Bash variable eval hash
Few days ago I needed to create a variable variable in Bash but I didn't find a way
to do in my Google search. After some attempts I could figure it out.
Maybe the approach showed isn't the best way to do it, so, if you know how to improve it
please let me know.
to do in my Google search. After some attempts I could figure it out.
Maybe the approach showed isn't the best way to do it, so, if you know how to improve it
please let me know.
name="Sergio"Other solution based on a Peter Lowe feedback:
eval `echo $name"_Address"`="Campinas/Brazil"
# printing...
echo $Sergio_Address
# or
eval echo $`echo $name"_Address"`
name="Sergio"
varname=$name"_Address"
eval $varname="Campinas/Brazil"
# printing...
echo $Sergio_Address
# or
echo ${!varname}