国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > env 以获得更好的体验。

# Bash 的 env 命令

env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

!subtitle:功能

创建新的环境运行程序。

!subtitle:类型

可执行文件(/usr/bin/env),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • -i, --ignore-environment - 忽略原本的环境变量,从空的环境开始

    • -0, --null - 以空字符(\0)作为行的结尾,而不是换行符(\n

    • -u, --unset=NAME - 删除环境变量 NAME

    • -C, --chdir=DIR - 切换工作目录

    • -S, --split-string=S - 将 S 按空格拆分为独立的参数

    • --block-signal[=SIG] - 阻止向执行的命令发送 SIG 信号

    • --default-signal[=SIG] - 将 SIG 信号的处理方式重置为默认

    • --ignore-signal[=SIG] - 将 SIG 信号的处理方式设为忽略

    • --list-signal-handling - 将处理方式不为默认的所有信号在标准错误中列出

    • -v, --debug - 打印每个处理步骤的详细信息

    • --help - 显示帮助

    • --version - 显示版本

  • - - 在所有选项之后,忽略原本的环境变量;同 -i 选项

  • NAME=VALUE - 要设置的环境变量

  • COMMAND - 要执行的命令,如果为空则打印所有环境变量

  • ARG - 命令 COMMAND 的参数,在 shebang 行中需要使用 -S 选项

!subtitle:返回值

  • 125 - env 命令自身失败

  • 126 - COMMAND 无法被调用

  • 127 - COMMAND 无法被找到

  • 其它 - COMMAND 的返回值

# 示例

!subtitle:设置环境变量

设置环境变量:

$ env NAME=Primers bash -c 'echo $NAME use $LANG'
Primers use en_US.UTF-8
$ NAME=Primers bash -c 'echo $NAME use $LANG'       # 可以不使用 env
Primers use en_US.UTF-8
  • 注意,这里不能直接写作 env NAME=Primers echo Hello $NAME,因为这样会在执行命令之前就将 $NAME 展开为空

  • 单纯设置环境变量可以不使用 env,直接在命令前设置即可

清空并设置环境变量:

$ env -i NAME=Primers bash -c 'echo $NAME use $LANG'
Primers use
  • 原有的环境变量被清空,因此 $LANG 为空

!subtitle:查看环境

$ env | grep PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
$ env | grep -i proxy
ALL_PROXY=http://localhost:7890

!subtitle:#!/usr/bin/env -S

#!/usr/bin/env -S bash -c
echo Hello World

如果不使用 -S 选项,bash -c 会被视为一个整体命令而失败:

/usr/bin/env: ‘bash -c’: No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines

# 推荐阅读

# 手册

ENV(1)                           User Commands                          ENV(1)

NAME
       env - run a program in a modified environment

SYNOPSIS
       env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

DESCRIPTION
       Set each NAME to VALUE in the environment and run COMMAND.

       Mandatory  arguments  to  long  options are mandatory for short options
       too.

       -i, --ignore-environment
              start with an empty environment

       -0, --null
              end each output line with NUL, not newline

       -u, --unset=NAME
              remove variable from the environment

       -C, --chdir=DIR
              change working directory to DIR

       -S, --split-string=S
              process and split S into separate arguments; used to pass multi‐
              ple arguments on shebang lines

       --block-signal[=SIG]
              block delivery of SIG signal(s) to COMMAND

       --default-signal[=SIG]
              reset handling of SIG signal(s) to the default

       --ignore-signal[=SIG]
              set handling of SIG signal(s) to do nothing

       --list-signal-handling
              list non default signal handling to stderr

       -v, --debug
              print verbose information for each processing step

       --help display this help and exit

       --version
              output version information and exit

       A mere - implies -i.  If no COMMAND, print the resulting environment.

       SIG may be a signal name like 'PIPE', or a  signal  number  like  '13'.
       Without  SIG,  all known signals are included.  Multiple signals can be
       comma-separated.  An empty SIG argument is a no-op.

   Exit status:
       125    if the env command itself fails

       126    if COMMAND is found but cannot be invoked

       127    if COMMAND cannot be found

       -      the exit status of COMMAND otherwise

OPTIONS
   -S/--split-string usage in scripts
       The -S option allows specifying multiple parameters in a script.   Run‐
       ning a script named 1.pl containing the following first line:

              #!/usr/bin/env -S perl -w -T
              ...

       Will execute perl -w -T 1.pl .

       Without the '-S' parameter the script will likely fail with:

              /usr/bin/env: 'perl -w -T': No such file or directory

       See the full documentation for more details.

   --default-signal[=SIG] usage
       This  option  allows  setting  a  signal handler to its default action,
       which is not possible using the traditional shell  trap  command.   The
       following  example  ensures  that  seq will be terminated by SIGPIPE no
       matter how this signal is being handled in  the  process  invoking  the
       command.

              sh -c 'env --default-signal=PIPE seq inf | head -n1'

NOTES
       POSIX's exec(3p) pages says:
              "many  existing applications wrongly assume that they start with
              certain signals set to the default action  and/or  unblocked....
              Therefore,  it is best not to block or ignore signals across ex‐
              ecs without explicit reason to do  so,  and  especially  not  to
              block  signals  across execs of arbitrary (not closely cooperat‐
              ing) programs."

AUTHOR
       Written by Richard Mlynarik, David MacKenzie, and Assaf Gordon.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright © 2023 Free Software Foundation, Inc.   License  GPLv3+:  GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This  is  free  software:  you  are free to change and redistribute it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       sigaction(2), sigprocmask(2), signal(7)

       Full documentation <https://www.gnu.org/software/coreutils/env>
       or available locally via: info '(coreutils) env invocation'

GNU coreutils 9.4                 April 2024                            ENV(1)
本文 更新于: 2025-11-27 09:38:14 创建于: 2025-11-27 09:38:14