The reason it uses env -i
is to clean out the environment variables before executing the rest of the command.
To really answer your question though, consider Windows:
In Windows, they give you "CreateProcess" which seems like a very sensible way to launch a new program. But the problem with CreateProcess is there are dozens of settings you might want to set for the new process, and for each setting you would need another parameter to the CreateProcess function call. This limits how much control the parent has over the child.
In Unix, they came up with the idea that one process first clones itself (inheriting all process settings) and then the second copy can change its settings before finally replacing itself with the new program. This lets you use any/all system calls to change things like current directory, environment, file handles, open sockets, signal masks, and so on without needing to add each one of these as a parameter to something like CreateProcess.
Then, a neat ability is to "chain" programs that perform different startup actions. Each program changes something about itself, then "exec"s into the next program. env
is one of these programs. It modifies its own environment, then execs another program. See chpst for a great example of all the things you can change in the program you want to start.
env -i sleep 50
in one terminal, and then runps fx
in another terminal and you will see that "sleep" is a child process of "bash" and "env" is not there. – dataless Mar 07 '16 at 22:21exec -c
instead ofenv -i
? Doesn'texec -c
clean out the environment too? The real benefit seems to not be the clearing of the environment but the ability to pass on some option (for example a PATH) to the new clean environment. – Pro Backup Dec 02 '16 at 14:28env -i
was what the original question asked about and setting variables wasn't mentioned, and my reply was about the nature of chain-execing programs and why this is useful. Alsoexec -c
might not be POSIX? (i.e. is specific to bash) – dataless Dec 07 '16 at 09:19