The -i
option to env
does in fact clear $PATH
out of the environment. The reason you see inconsistent results from the system
call is because of how system
works:
If there is only one scalar argument, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing (this is /bin/sh -c on Unix platforms, but varies on other platforms). If there are no shell metacharacters in the argument, it is split into words and passed directly to execvp , which is more efficient.
Because you are calling system
with a single commandline that must be parsed by a shell, one is started, which initializes its own $PATH
upon startup (because shells do that), and that's the one you're seeing.