使用env为bash中的一个程序调用设置环境变量 [英] Setting environment variable for one program call in bash using env
问题描述
我尝试通过命令 env
调用具有修改环境的shell命令。
根据手册
env HELLO ='Hello World'echo $ HELLO
应该回显 Hello World
,但没有。
如果我做
HELLO ='Hello World'bash -c'echo $ HELLO'
它按预期打印 Hello World
(感谢这个信息的答案)。
我在这里缺少什么? p>
干杯,
尼克拉斯
情况下,您的当前shell在运行命令之前扩展$ HELLO变量。而您当前的shell中没有设置HELLO变量。
env HELLO ='Hello World'echo $ HELLO
/ p>
将这样做:
- 展开任何给定的变量,在这种情况下为$ HELLO
- 运行env与3个参数'HELLO = Hello World','echo'和''(一个空字符串,因为在当前shell中没有设置HELLO变量)
- env命令将在其环境中运行并设置HELLO ='Hello World'
- env将使用参数'(空字符串)运行echo / li>
如你所见,当前的shell扩展了$ HELLO变量,没有设置。
HELLO ='Hello World'bash -c'echo $ HELLO'
会做这个:
- 设置变量
HELLO ='Hello World
/ li>
- 运行bash与2个参数'-c'和'echo $ HELLO'
- 最后一个参数用单引号括起来,其中的任何内容都不会展开
- 新的bash依次运行命令
echo $ HELLO
- 要在新的bash子shell中运行echo $ HELLO,bash首先扩展它可以使用的任何东西,在这种情况下,$ HELLO,并将父shell设置为Hello World我们。
- subshell运行echo'Hello World'
如果你试图做例如这个:
env HELLO ='Hello World'echo'$ HELLO'
- 当前的shell将扩展任何可以的东西,这是没有,因为$ HELLO被包含在单引号
- 运行env与3参数'HELLO = Hello World','echo'和'$ HELLO'
- env命令将在其环境中运行并设置HELLO ='Hello World'
- env将使用参数'$ HELLO'运行echo。
在这种情况下,没有shell可以扩展$ HELLO,所以echo接收字符串 $ HELLO
并打印出来。变量扩展仅由shell完成。
I am trying to invoke a shell command with a modified environment via the command env
.
According to the manual
env HELLO='Hello World' echo $HELLO
should echo Hello World
, but it doesn't.
If I do
HELLO='Hello World' bash -c 'echo $HELLO'
it prints Hello World
as expected (thanks to this answer for this info).
What am I missing here?
Cheers, Niklas
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.
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 HELLO variable set in the current shell)
- The env command will run and set the HELLO='Hello World' in its environment
- env will run echo with the argument '' (an empty string)
As you see, the current shell expanded the $HELLO variable, which isn't set.
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, anything inside it is not 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 to "Hello World" for us.
- The subshell runs 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'
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.
这篇关于使用env为bash中的一个程序调用设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!