It lives in your shell's memory, and is copied into the memory of any commands that your shell executes. The environment is a property of the running process and is never written to disk; if you export WEBROOT=blah
, exit your shell, and then restart it, you'll see that WEBROOT
is now empty. (You can check this with printenv
or just echo $WEBROOT
.)
Several shell startup scripts, such as .bashrc
or .bash_profile
, contain commands to initialize certain environment variables to specified values. But these are simply recordings of export
commands as you would run yourself at the prompt. They do not store the environment itself.
If you write C code, you may be familiar with the standard prototype for the main
function:
int main(int argc, char** argv) {
// ...
}
But actually, there is another argument to main
that is typically omitted:
int main(int argc, char** argv, char** envp) {
// ...
}
This envp
is where the environment lives -- it's set up by the kernel and pushed onto the stack above main
, just like the program arguments. The standard Unix environment also provides the environ
pointer which points to this area of memory, for usage by libc functions such as getenv
and setenv
.
When a program calls exec
, the environment is also passed into that call, to be copied into the initial stack of the exec
'd program. The full prototype for the syscall, execve
, can be seen to take this pointer:
int execve(const char *path, char *const argv[], char *const envp[]);