bash 使用env为bash中的一个程序调用设置环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13998075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:07:48  来源:igfitidea点击:

Setting environment variable for one program call in bash using env

bashunixenvironment-variablesenvironment

提问by Niklas

I am trying to invoke a shell command with a modified environment via the command env.

我正在尝试通过命令调用具有修改环境的 shell 命令env

According to the manual

根据手册

env HELLO='Hello World' echo $HELLO

should echo Hello World, but it doesn't. If I do

应该 echo Hello World,但它没有。如果我做

HELLO='Hello World' bash -c 'echo $HELLO'

it prints Hello Worldas expected (thanks to this answerfor this info).

Hello World按预期打印(感谢此信息的答案)。

What am I missing here?

我在这里缺少什么?

Cheers, Niklas

干杯,尼克拉斯

回答by nos

It's because in your first case, your current shell expands the $HELLOvariable before running the commands. And there's no HELLOvariable set in your current shell.

这是因为在您的第一种情况下,您当前的 shell$HELLO在运行命令之前扩展了变量。并且HELLO您当前的 shell 中没有设置变量。

env HELLO='Hello World' echo $HELLO

will do this:

会这样做:

  • expand any variables given, in this case $HELLO
  • run env with the 3 arguments 'HELLO=Hello World', 'echo'and ''(an empty string, since there's no HELLOvariable set in the current shell)
  • The envcommand will run and set the HELLO='Hello World'in its environment
  • envwill run echowith the argument ''(an empty string)
  • 扩展给定的任何变量,在这种情况下 $HELLO
  • 与3个参数运行env 'HELLO=Hello World''echo'以及''(一个空字符串,由于没有HELLO变量集合在当前shell)
  • env命令将运行并HELLO='Hello World'在其环境中设置
  • envecho使用参数''(空字符串)运行

As you see, the current shell expanded the $HELLOvariable, which isn't set.

如您所见,当前 shell 扩展了$HELLO未设置的变量。

HELLO='Hello World' bash -c 'echo $HELLO'

will do this:

会这样做:

  • set the variable HELLO='Hello Worldfor the following command
  • run bash with the 2 arguments '-c'and 'echo $HELLO'
  • since the last argument is enclosed in single quotes, nothing inside it is expanded
  • the new bash in turn will run the command echo $HELLO
  • To run echo $HELLOin the new bash sub-shell, bash first expands anything it can, $HELLOin this case, and the parent shell set that to Hello Worldfor us.
  • The subshell runs echo 'Hello World'
  • HELLO='Hello World为以下命令设置变量
  • 使用 2 个参数运行 bash'-c''echo $HELLO'
  • 因为最后一个参数用单引号括起来,所以里面没有展开
  • 新的 bash 反过来将运行命令 echo $HELLO
  • 为了$HELLO在新的 bash 子 shell 中运行 echo ,bash 首先扩展它可以扩展的任何东西,$HELLO在这种情况下,父 shellHello World为我们设置它。
  • 子shell运行 echo 'Hello World'

If you tried to do e.g. this:

如果您尝试这样做,例如:

env HELLO='Hello World' echo '$HELLO'
  • The current shell would expand anything it can, which is nothing since $HELLOis enclosed in single quotes
  • run env with the 3 arguments 'HELLO=Hello World', 'echo'and '$HELLO'
  • The env command will run and set the HELLO='Hello World'in its environment
  • env will run echo with the argument '$HELLO'
  • 当前的 shell 会扩展它所能扩展的任何东西,因为$HELLO用单引号括起来,所以什么都没有
  • 使用 3 个参数运行 env 'HELLO=Hello World''echo'以及'$HELLO'
  • env 命令将HELLO='Hello World'在其环境中运行并设置
  • env 将使用参数运行 echo '$HELLO'

In this case, there's no shell that will expand the $HELLO, so echoreceives the string $HELLOand prints out that. Variable expansion is done by shells only.

在这种情况下,没有外壳可以扩展$HELLO,因此echo接收字符串$HELLO并打印出来。变量扩展仅由壳完成。

回答by Lev Levitsky

I think what happens is similar to this situationin which I was also puzzled.

我认为发生的事情与我也感到困惑的这种情况相似。

In a nutshell, the variable expansion in the first case is done by the current shell which doesn't have $HELLOin its environment. In the second case, though, single quotes prevent the current shell from doing the variable expansion, so everything works as expected.

简而言之,第一种情况下的变量扩展是由当前 shell 完成的$HELLO,它的环境中没有。但是,在第二种情况下,单引号会阻止当前 shell 进行变量扩展,因此一切都按预期进行。

Note how changing single quotes to double quotes prevents this command from working the way you want:

请注意将单引号更改为双引号如何阻止此命令按您想要的方式工作:

HELLO='Hello World' bash -c "echo $HELLO"

Now this will be failing for the same reason as the first command in your question.

现在这将因与您问题中的第一个命令相同的原因而失败。

回答by deFreitas

This works and is good for me

这对我有用

$ MY_VAR='Hello' ANOTHER_VAR='World!!!' && echo "$MY_VAR $ANOTHER_VAR"
Hello World!!!

回答by William Entriken

Here is an easier way to confirm shell is working as expected.

这是确认外壳按预期工作的更简单方法。

env A=42 env
env

The first command runs sets Ato 42 and runs env. The second command also runs runs env. Compare the output of both.

第一个命令运行设置A为 42 并运行env. 第二个命令也运行 running env。比较两者的输出。