简体   繁体   English

“ env” bash中“ echo”的奇怪行为

[英]strange behavior of 'echo' in 'env' bash

When I am trying echo variable inside 'env' command I got nothing, but I can see it using 'printenv' command: 当我在'env'命令中尝试echo变量时,什么也没得到,但是我可以使用'printenv'命令看到它:

root@devel:~# env xxx=23 echo $xxx

root@devel:~# env xxx=23 printenv | grep xxx
xxx=23

what's wrong here? 怎么了

env xxx=23 echo $xxx

In the above, the shell evaluates $xxx before env is executed. 在上面,shell在执行env之前评估$xxx Thus, nothing is echoed. 因此,没有回声。

In more detail, the shell sees the four words env , xxx=23 , echo and $xxx . 更详细地讲,外壳程序会看到四个单词envxxx=23echo$xxx It interprets env as a command name and xxx=23 , echo , and $xxx as three arguments which will be passed to the command env . 它将env解释为命令名称,并将xxx=23echo$xxx为三个参数,这些参数将传递给命令env It evaluates $xxx before passing it to the command env . $xxx传递给命令env 之前,它会求值。

By contrast, in the following, there are no shell variables for the shell to evaluate. 相比之下,在下文中,没有要评估的Shell变量。 Instead env is executed with two arguments, xxx=23 and printenv . 而是用两个参数xxx=23printenv执行env env sets the environment variable xxx and then executes printenv : env设置环境变量xxx ,然后执行printenv

$ env xxx=23 printenv | grep xxx
xxx=23

Similarly, observe: 同样,请注意:

$ env xxx=23 sh -c 'echo $xxx'
23

Since $xxx is inside single-quotes, the shell does not evaluate it. 由于$xxx在单引号内,因此外壳程序不会对其进行评估。 Instead is runs env with four arguments: xxx=23 , sh , -c , and echo $xxx . 而是使用四个参数运行envxxx=23sh-cecho $xxx After env sets environment variable xxx , it executes sh with arguments -c and echo $xxx . env设置环境变量xxx ,它将使用参数-c执行shecho $xxx The $xxx is evaluated when sh is executed and hence it sees the variable xxx . $xxx sh在执行sh时被求值,因此它看到变量xxx

When you run env xxx=23 echo $xxx , the variable xxx=23 becomes visible to the echo process during its execution. 当您运行env xxx=23 echo $xxx ,变量xxx=23在执行过程中对echo过程可见。 But in echo $xxx the value of $xxx is not evaluated by echo , it is evaluated by the currently executing shell. 但在echo $xxx价值$xxx不会被评估echo ,它是由当前正在执行的外壳进行评估。 And since the env ... invocation doesn't affect the current shell, the value of $xxx is whatever it was before you executed this command (probably unset). 而且由于env ...调用不会影响当前的shell,因此$xxx的值与执行此命令之前的值相同(可能未设置)。

echo is not a good way to test the effect of env , because you cannot make the echo command print a specific value defined in its environment. echo不是测试env效果的好方法,因为您不能使echo命令打印在其环境中定义的特定值。 Your example with printenv is better, because it dumps the content of environment variables it knows about. 使用printenv示例更好,因为它转储了它所知道的环境变量的内容。 Another good test is what @john wrote in his answer, invoking another shell and make the shell print a chosen environment variable. 另一个好的测试是@john在他的答案中写的内容,它调用另一个shell并使该shell打印一个选定的环境变量。 Any program with the ability to print the content of environment variables would work. 任何能够打印环境变量内容的程序都可以使用。

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

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