前缀变量分配不适用于echo [英] Prefixing variable assignment doesn't work with echo
本文介绍了前缀变量分配不适用于echo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
据我所知,如果为bash命令添加变量分配前缀,则该变量将立即生效.而且只会在该命令中生效.
As far as I know, if you prefix a bash command with variable assignment the variable will take effect immediately. And it will only have effect within that command.
我在bash中尝试了此命令:
I tried this command in bash:
V=1 echo $V
但是终端中没有打印出1,这不是我期望的.那么为什么它不起作用?
However 1 is not printed out in the terminal which is not what I expected. So why it doesn't work?
推荐答案
变量将在紧随其后的命令环境中传递,而不是在评估(扩展)命令时传递.任何变量扩展都将由Shell提前完成.
The variable will be passed in the environment of the command following it, not when the command is being evaluated (expanded). Any variable expansion will be done earlier by shell.
$ V=1 env | grep V=
V=1
要使其正常工作:
$ V=1; echo $V
1
这篇关于前缀变量分配不适用于echo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文