The following script shows the actual value of “$()
” and “${}
” first, and then put all “$()
” and “${}
” inside a variable “x
” as a string contains some bash expressions
inside, finally, try to use “$(eval)
” to convert all “$()
” and “${}
” into actual value, and then set the result to “y
“.
# echo "id: $(id)"
id: uid=0(root) gid=0(root) groups=0(root)
# echo "whoami: $(whoami)"
whoami: root
# x='hello world: "$(id)", whoami: "$(whoami)"'
# echo "x: $x"
x: hello world: "$(id)", whoami: "$(whoami)"
y="$(eval "$x")"
echo "y: $y"
I expect the $(eval)
can evaluate any given string and auto parse all $()
and ${}
inside that string, but got the following error when executing the y="$(eval "$x")"
:
Command 'hello' not found, but can be installed with:
apt install hello
Here is the expected output:
y: hello world: uid=0(root) gid=0(root) groups=0(root), whoami: root
How to replace all “$()
” and “${}
” inside a bash variable “x
“?
1