使用 env 在 bash 中为一个程序调用设置环境变量 [英] Setting environment variable for one program call in bash using env

查看:39
本文介绍了使用 env 在 bash 中为一个程序调用设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

根据说明书

env HELLO='Hello World' echo $HELLO

应该回显 Hello World,但它没有.如果我这样做了

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

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

它按预期打印 Hello World(感谢 this answer 获取此信息).

it prints Hello World as expected (thanks to this answer for this info).

我在这里错过了什么?

干杯,尼克拉斯

推荐答案

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

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

会这样做:

  • 展开任何给定的变量,在本例中为 $HELLO
  • 使用 3 个参数运行 env 'HELLO=Hello World''echo'''(一个空字符串,因为有当前shell中没有设置HELLO变量)
  • env 命令将运行并在其环境中设置 HELLO='Hello World'
  • env 将使用参数 ''(空字符串)运行 echo
  • 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)

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

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

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

会这样做:

  • 为以下命令设置变量HELLO='Hello World
  • 使用 2 个参数 '-c''echo $HELLO'
  • 运行 bash
  • 因为最后一个参数是用单引号括起来的,所以里面没有任何东西被展开
  • 新的 bash 将依次运行命令 echo $HELLO
  • 要在新的 bash 子 shell 中运行 echo $HELLO,bash 首先扩展它可以扩展的任何内容,在这种情况下为 $HELLO,然后父 shell 将其设置为Hello World 为我们服务.
  • 子shell运行echo 'Hello World'
  • 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 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'

  • 当前的 shell 会扩展它可以扩展的任何东西,这没什么,因为 $HELLO 用单引号括起来
  • 使用 3 个参数 'HELLO=Hello World''echo''$HELLO'
  • 运行 env
  • env 命令将运行并在其环境中设置 HELLO='Hello World'
  • env 将使用参数 '$HELLO'
  • 运行 echo

    • 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,所以 echo 接收字符串 $HELLO 并打印出来.变量扩展仅由 shell 完成.

      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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆