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
Setting environment variable for one program call in bash using env
提问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 World
as 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 $HELLO
variable before running the commands. And there's no HELLO
variable 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 noHELLO
variable set in the current shell) - The
env
command will run and set theHELLO='Hello World'
in its environment env
will runecho
with the argument''
(an empty string)
- 扩展给定的任何变量,在这种情况下
$HELLO
- 与3个参数运行env
'HELLO=Hello World'
,'echo'
以及''
(一个空字符串,由于没有HELLO
变量集合在当前shell) - 该
env
命令将运行并HELLO='Hello World'
在其环境中设置 env
将echo
使用参数''
(空字符串)运行
As you see, the current shell expanded the $HELLO
variable, which isn't set.
如您所见,当前 shell 扩展了$HELLO
未设置的变量。
HELLO='Hello World' bash -c 'echo $HELLO'
will do this:
会这样做:
- set the variable
HELLO='Hello World
for 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
$HELLO
in the new bash sub-shell, bash first expands anything it can,$HELLO
in this case, and the parent shell set that toHello World
for 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
$HELLO
is 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 echo
receives the string $HELLO
and 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 $HELLO
in 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 A
to 42 and runs env
. The second command also runs runs env
. Compare the output of both.
第一个命令运行设置A
为 42 并运行env
. 第二个命令也运行 running env
。比较两者的输出。