How can I expand a variable, run that command and store output to variable?
Usually you do this
var="$(echo string)"
but I want this
envString='echo $stringToEcho'
stringToEcho="hello world"
var="$(${envString})"`
but the dollar sign inside doesn't expand. I need to run a command stored in a variable and store the output to another variable.
If your string contains content which was written to be
eval
-safe:...which will work even if it includes variable references, if quoting correctly:
However, if your string contains contents generated by expanding variables without using
printf %q
to safely escape any variables contained, do not do this.References: