重击:在同一行上分配变量不会改变回声行为 [英] Bash: assignment of variable on same line not altering echo behavior
问题描述
a=2
a=3 echo $a #prints 2
有人可以解释为什么有人在第二行中使用上面的代码吗?a = 3将被忽略,因为其后没有"enter".但是我在上面的脚本中看到了它,并且不确定目的.
can someone explain why would anyone use the above code in line-2. a=3 will be ignored as there is no "enter" after it. But I saw it in script like above and not sure about the purpose.
推荐答案
$ a
在评估 a = 3
之前由外壳程序(Bash)扩展.因此, echo
会将其参数显示为 2
,这就是它输出的内容.(如果您 set -x
,您会看到执行的是 a = 3 echo 2
.)
$a
is expanded by the shell (Bash) before a=3
is evaluated. So echo
sees its argument as 2
, which is what it prints. (If you set -x
you can see that what gets executed is a=3 echo 2
.)
var = val命令
用于设置环境变量,以便在执行过程中 command
可以看到,但无其他地方.因此,当 command
读取环境变量时(例如,使用 getenv()
), $ var
就是 val
.
var=val command
is used to set an environment variable to be seen by command
during its execution, but nowhere else. So when command
reads environment variables (e.g. using getenv()
), to it $var
is val
.
如果 echo
在运行时查找 $ a
,它将具有值 3
.
If echo
were to look up $a
while running, it would have the value 3
.
这篇关于重击:在同一行上分配变量不会改变回声行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!