I have read the linux command env
manual, specifically, for the -i
option, the manual says:
-i, --ignore-environment # start with an empty environment
What I get is that when -i
option is specified, the environment is empty,ie, no environment variable is there, so the command env -i ls
should print something like command not found , but what I see is the command executes successfully. So please explain, do I misunderstand anything?
I think it better to read the source code to find out whether the OS PATH variable persists through a clearing of environment – Tracy
Reading the source code would of course give all information about the matter. But we can also obtain valuable clues by using strace
:
>strace -eexecve env -i ls execve("/usr/bin/env", ["env", "-i", "ls"], [/* 48 vars */]) = 0 execve("ls", ["ls"], [/* 0 vars */]) = -1 ENOENT (No such file or directory) execve("/bin/ls", ["ls"], [/* 0 vars */]) = 0
We see that env
tries executing "ls"
without a path first, which fails, and then tries executing "/bin/ls"
, which succeeds. We also see it start with an empty environment [/* 0 vars */]
.
>strace -eexecve env -i foo execve("/usr/bin/env", ["env", "-i", "foo"], [/* 48 vars */]) = 0 execve("foo", ["foo"], [/* 0 vars */]) = -1 ENOENT (No such file or directory) execve("/bin/foo", ["foo"], [/* 0 vars */]) = -1 ENOENT (No such file or directory) execve("/usr/bin/foo", ["foo"], [/* 0 vars */]) = -1 ENOENT (No such file or directory) env: foo: No such file or directory
When specifying a non-existent command or a command residing in some other path, we see that env
finally tries the /usr/bin/
path, and that's it. So, obviously /bin/
and /usr/bin/
are hardcoded in env
, and with -i
the command's environment is indeed empty. Another test:
>env -i strace ls strace: ls: command not found
If ls
is to be executed not directly by env -i
, but indirectly thru another command, it is not found.
The environment refers to all of the shell variables you set in your .bashrc and .bash_profile on login. If you're compiling something, you may set the CFLAGS
variable. If you've got something installed in a weird directory, you may do an export PATH=$PATH:$HOME/build/bin
to allow it to run without specifying its full path.
env -i
clears all these user-set variables and gives you a blank environment, so you can, for example, test how a command works if you have none of these extra variables set.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.