The C library treats environment variables as global parameters that you can read using getenv and configure using setenv / putenv , and which inherit the calls to the exec family, but this is a convenient fiction. As for the kernel, environment variables are the second set of arguments to main . This becomes clear if you look at the actual execve system call underlying the exec family. This is his prototype C:
int execve(const char *filename, char *const argv[], char *const envp[]); ^^^^^^^^^^^^^^^^^^
See this third argument? This is the only way for process A to set the environment variables of process B. 1 Therefore, the only time process A can set the environment variables of process B when process A starts process B through fork and execve .
Your program is launched by the shell, so the shell must set its copy of the environment variables, but there is no way to return it back to the shell - just as there is no way for the C function to change the values โโof its arguments in its caller.
1 Do not lift ptrace .
source share