tl;dr

use eval "echo \${$FOO}"

#!/bin/sh

VALUE1="happy!"  # target value
ENV_VAR=VALUE1   # variable bname

echo $VALUE1  # => happy!
echo $ENV_VAR # => VALUE1
echo ${ENV_VAR} # => VALUE1

echo \${$ENV_VAR} # => ${VALUE1}

#=== ash or other shells ===#
eval "echo \${$ENV_VAR}"  # => happy!

#=== bash supports original indirect expantion, but not ash on Alpine linux ===#
echo ${!ENV_VAR}

#=== example for "if" "test" conditions ===#
test -n $ENV_VAR \  # (1)
&& test "$(eval "echo \${$ENV_VAR}")" == "happy!" \  # (2)
&& echo "I'm Happy!!"

# (1) if ENV_VAR is set,
# (2) and if $VALUE1 == "happy!"

Leave a Reply

Your email address will not be published. Required fields are marked *