Document Actions

How to create variable variables in bash

by Sérgio Oliveira Campos last modified 2008-09-02 21:38

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.

name="Sergio"
eval `echo $name"_Address"`="Campinas/Brazil"

# printing...
echo $Sergio_Address
# or
eval echo $`echo $name"_Address"`
Other solution based on a Peter Lowe feedback:

name="Sergio"
varname=$name"_Address"
eval $varname="Campinas/Brazil"

# printing...
echo $Sergio_Address
# or
echo ${!varname}


Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: