2 Answers
2

You have used system function, so it will use another shell to run the command which which. From man system:

DESCRIPTION
system() executes a command specified in command by calling /bin/sh -c
command, and returns after the command has been completed. During exe‐
cution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT
will be ignored.

i expected the subshell to inherit the empty path and therefore had no consequence. thank you for pointing out that missconception!
–
kepplaApr 19 '14 at 19:30

1

The subshell does inherit the empty path. However, as a feature of bash (and presumably other shells too), if it is started with PATH unset, it sets it to a default value, which is what we see in the echo $PATH example.
–
Nate EldredgeApr 20 '14 at 1:35

In an empty environment, executables are not found unless you specify the full path. Try it with execvp.

The system function invokes a shell — on Linux with Glibc, it invokes /bin/sh (so no PATH needed). Shells define a few variables of their own in addition to the ones coming from the environment. You can see what they define by running env -i /path/to/shell -c set, and which ones they export by running env -i /path/to/shell -c export. In particular, both dash and bash — the two shells that you're likely to find as /bin/sh on Linux — set (but do not export) PATH to a “sane” default if there isn't one in the environment. Different shells set different values or none at all.

On my machine (and yours, apparently), which is itself a /bin/sh script. The shell invoked by system uses its own path variable to find the which program, but does not export it. The shell that runs the which script itself also defines the PATH variable for its internal use.