Resolve bash variable containted in another variable
For security reasons it's best to avoid eval
. Something like this would be preferable:
TEXT_TO_FILTER='I would like to replace this %s to proper value'
var=variable
printf -v TEXT_AFTER_FILTERED "$TEXT_TO_FILTER" "$var"
# or TEXT_AFTER_FILTERED=$(printf "$TEXT_TO_FILTER" "$var")
echo "$TEXT_AFTER_FILTERED"
TEXT_AFTER_FILTERED="${TEXT_TO_FILTER//\$var/$var}"
or, using perl:
export var
TEXT_AFTER_FILTERED="$(echo "$TEXT_TO_FILTER" | perl -p -i -e 's/\$(\S+)/$ENV{$1} || $&/e')"
This is still more secure than eval.