Evaluating a variable

Does anyone know of a way to force a variable name held in another variable to return the value of the first variable? Best if I give an example, that does not work:

/usr/local/bin >cat mike.sh
NUM1ref=16
NUM2ref=32
echo "=============="
for VAR in NUM1 NUM2
do
  XXXX=${VAR}ref
  echo $XXXX
  echo "=============="
done
/usr/local/bin >./mike.sh 
==============
NUM1ref
==============
NUM2ref
==============
/usr/local/bin >

I want this to echo the value held in NUM1ref (16), rather than the string "NUM1ref", so the variable XXXX has to be evaluated twice.

I hope that makes sense!

bash, how to get variable name from variable

If you haven't got bash.

NUM1ref=16
NUM2ref=32
echo "=============="
for VAR in NUM1 NUM2
do
  eval echo \$${VAR}ref
  echo "=============="
done

==============
16
==============
32
==============
1 Like

Thanks Methyl, that does it.
Out of interest, if I have got a bash shell ...?

Regards, Mike

(See the link in bartus11 post - it shows the special syntax for bash).