[英]Why does `env -i` fail to ignore my environment?
Acknowledgement: Maybe I'm misunderstanding what "environment" means.致谢:也许我误解了“环境”的含义。
The man page for env
claims that env - [command]
(or env -i [command]
) should run command
in an empty environment. env 的
env
页声称env - [command]
(或env -i [command]
)应该在空环境中运行command
。 Testing this behavior, PATH
is an environment variable (ie a part of an "environment").测试这种行为,
PATH
是一个环境变量(即“环境”的一部分)。 But, alas:可惜:
➜ ~ env - echo $PATH
/home/user/.nvm/versions/node/v15.3.0/bin:/home/user/.deno/bin:/usr/local/lib/node/nodejs/bin:/home/user/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
is the same as是相同的
➜ ~ echo $PATH
/home/user/.nvm/versions/node/v15.3.0/bin:/home/user/.deno/bin:/usr/local/lib/node/nodejs/bin:/home/user/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
So, what's going on?发生什么了? This happens for a few other environment variables, too, like
$SHELL
and $USER
.其他一些环境变量也会发生这种情况,例如
$SHELL
和$USER
。
So, can someone define "environment" and what env
actually does when using env - [command]
or env -i [command]
.那么,有人可以定义“环境”以及
env
在使用env - [command]
或env -i [command]
时实际做了什么。
Note that env - PATH= echo $PATH
produces the expected result of failing to find echo
on the path.请注意,
env - PATH= echo $PATH
会产生无法在路径上找到echo
的预期结果。 But, it's weird to me that I have to explicitly set that environment variable to an empty string since I'm ostensibly clearing my environment prior to running [command]
.但是,我必须将该环境变量显式设置为空字符串,这对我来说很奇怪,因为我表面上是在运行
[command]
之前清除我的环境。
Bonus confusion:奖金混乱:
env env
prints all of my environment's variables while env - env
produces empty output (makes sense... environment should be i
gnored, but this seems inconsistent with env - echo $PATH
printing my environment's PATH
.) env env
打印我的所有环境变量,而env - env
产生空的i
(有道理... environment 应该被忽略,但这似乎与env - echo $PATH
打印我的环境的PATH
不一致。)
In env -i echo $PATH
, the variable is expanded by your shell, so the env
program never sees the variable.在
env -i echo $PATH
中,该变量由您的 shell 扩展,因此env
程序永远不会看到该变量。
You can spawn a child shell to inspect variables;您可以生成一个子 shell 来检查变量; use quotes to prevent evaluation by the parent shell:
使用引号防止父 shell 评估:
A=1 env -i sh -c 'echo $A' # prints nothing
A=1 env sh -c 'echo $A' # prints 1
A=1 env printenv A # prints 1
PATH is special. PATH 很特别。 Even if you delete it from your environment, your shell will probably define a fallback path.
即使您从您的环境中删除它,您的 shell 也可能会定义一个后备路径。 That one is not exported to child processes but only visible as shell variable.
那一个不会导出到子进程,但仅作为 shell 变量可见。
env -i printenv PATH # prints nothing, PATH is no longer exported
env -i sh -c 'echo $PATH' # prints something like /sbin:/bin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.