Use the declare
built-in along with indirect-variable expansion in bash
. First define the elements of for the dynamic nature in an array as
#!/bin/bash
list=(one two)
unset count
for var in "${list[@]}"; do
declare var_${var}_string="string"$((++count))
done
and now access the created variables using indirect expansion
for var in "${list[@]}"; do
dymv="var_${var}_string"
echo "${!dymv}"
done
and never use eval
for this requirement. Could be dangerous because of unlikely code-injection possibility with a harmful command.