POSIX – Indirect Variable Expansion in POSIX as Done in Bash

posixshellshell-script

Is it possible, or is there some elegant hack to do indirect variable expansion in POSIX as can be done in Bash?

For context, I'm trying to do the following:

for key in ${!map_*}
do
    # do something
done

EDIT: To clarify, I'd like to access shell variables that begin with map_.

Best Answer

The hack is to use eval:

aaa=1
aab=2
aac=3

eval_like() {
    pattern=$1
    vars=`set |grep "^$pattern.*=" | cut -f 1 -d '='`
    for v in $vars; do
        eval vval="\$$v"
        echo $vval
    done
}   

for i in `eval_like aa`; do
    echo $i
done
Related Question