nathan
nathan

Reputation: 844

bash variable evaluation on command substitution

Below is a piece of bash code

2 bar=false
3 foo=$(echo $bar);
4 echo $foo
5
6 echo change bar from false to true
7
8 bar=true
9 echo $foo

Below is output

false
change a from false to true
false

I was expecting line 9 echo command gonna re-execute the command substitution and output true. However it is not. the second $foo would directly refer to "foo" value, which is literal "false", instead of doing command execution again. Well, that is reasonable to design like this. Am I guessing right ? Is there any behind-the-scene mechanism about this behavior

Upvotes: 1

Views: 117

Answers (1)

SLePort
SLePort

Reputation: 15461

foo=$(echo $bar); is an assignement, not function that is re-evaluated when you later change the value of bar.

foo is just set here with the output of the command substitution and the value is false.

Upvotes: 2

Related Questions