The bash man says that variable expansion occurs before command substitution. I was seeking of an example that shows it clearly. So i tried this:
root@antec:/# var=1
root@antec:/# echo $(var=2; echo $var)
2
root@antec:/#
I was expecting bash to do:
1) replace $var by "1" in the substitution
2) execute echo $(var=2; echo 1)
Obviously this is not what bash is doing ..
Can someone please explain what is going one here ? And if someone has an example showing the precedence of variable expansion over command substitution it would be nice too
I do not know what the bash man page is talking about.
The POSIX specification for the shell says:
The order of word expansion shall be as follows:
- Tilde expansion, parameter expansion, command substitution, and arithmetic expansion shall be performed, beginning to end.
- Field splitting shall be performed on the portions of the fields generated by step 1, unless IFS is null.
- Pathname expansion shall be performed, unless set -f is in effect.
- Quote removal shall always be performed last.
This makes it pretty clear that variable expansion (aka. "parameter expansion") happens at the same time as command expansion ("command substitution"), not before or after.
So I do not think the example you are asking for exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With