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
If your string contains content which was written to be eval
-safe:
envString="echo string"
var=$(eval "$envString")
...which will work even if it includes variable references, if quoting correctly:
envString='echo "$someVar"' # use single-quotes here to avoid premature expansion!
someVar='hello world'
var=$(eval "$envString")
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: