Linux env -i 奇数

Linux env -i oddity

这按预期工作:

[dgorur@ted ~]$ env -i env
[dgorur@ted ~]$

这样做:

[dgorur@ted ~]$ env -i which date
which: no date in ((null))

但是看看这个:

[dgorur@ted ~]$ env -i  date
Fri Aug 28 22:27:15 PDT 2015

有意思。这个怎么样:

[dgorur@ted ~]$ env -i  whereis date
date: /bin/date /opt/rocks/bin/date /usr/share/man/man1p/date.1p.gz /usr/share/man/man1/date.1.gz

这是怎么回事?一个空的环境是否意味着人们认为它的作用,或者某些命令很特别?哦,ls 也可以。

"empty environment" 并非如此。

the source codeenv,可以看到它运行:

execvp (argv[optind], &argv[optind]);

如果一个反过来读取 man execvp,如果您不提供一个,它会发现它会创建自己的 PATH:

The execlp(), execvp(), and execvpe() functions duplicate the actions of the shell in searching for an executable file if the specified filename does not contain a slash (/) character. The file is sought in the colon-separated list of directory pathnames specified in the PATH environment variable. If this variable isn't defined, the path list defaults to the current directory followed by the list of directories returned by confstr(_CS_PATH). (This confstr(3) call typically returns the value "/bin:/usr/bin".) [emphasis added.]

因此,默认 PATH 中的程序可以正常工作:

$ env -i env
$ env -i which date
/bin/date
$ env -i  date
Fri Aug 28 23:12:21 PDT 2015

(我的 env -i which date 的行为与你的不同,可能是由于 which 的实施。)

在我的测试中,默认的 PATH 包括当前目录。但是,任何不在系统默认 PATH 中的命令,例如 ~/bin 中的命令,在 env -i.

下都会失败