Changing environment variable in UNIX

Tag: unix Author: y0o0y Date: 2009-09-10

If we change an environment variable in one process, is the change reflected in other running processes as well?

Best Answer

No. Each process has its own copy of the environment variables. Child processes can inherit environment variables from their parent processes, but these are copies, and so are not altered if the parent's versions are modified.

comments:

Nor is the parent's copy altered if the child alters their environment - the two are independent.

Other Answer1

No. The change is only passed on to child processes, and only those started after the change is made. From Wikipedia:

In all Unix and Unix-like systems, each process has its own private set of environment variables. By default, when a process is created it inherits a duplicate environment of its parent process, except for explicit changes made by the parent when it creates the child.

Other Answer2

An exception is when you invoke a shell script through the . *filename* [*args*] or source *filename* [*args*] syntax. Any changes made to the environment in these subprocesses scripts are also reflected in the original environment.

comments:

... because it's not a subprocess, it's being interpreted by the shell in the same process.