简体   繁体   English

bash变量评估中的命令替换

[英]bash variable evaluation on command substitution

Below is a piece of bash code 下面是一段bash代码

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. 我期望第9行echo命令将重新执行命令替换并输出true。 However it is not. 但是事实并非如此。 the second $foo would directly refer to "foo" value, which is literal "false", instead of doing command execution again. 第二个$ foo将直接引用“ foo”值,即字面量“ false”,而不是再次执行命令。 Well, that is reasonable to design like this. 好吧,这样设计是合理的。 Am I guessing right ? 我猜对了吗? Is there any behind-the-scene mechanism about this behavior 是否有关于此行为的幕后机制

foo=$(echo $bar); is an assignement, not function that is re-evaluated when you later change the value of bar . 是分配,而不是功能,在以后更改bar的值时会重新评估。

foo is just set here with the output of the command substitution and the value is false . 仅在此处使用命令替换的输出设置foo ,并且该值为false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM