Changing the environment in sh

Steve Dyer dyer at spdcc.COM
Sat Sep 3 03:37:15 AEST 1988


There is no way to change a shell environment variable directly
from a child process, because the environment is an attribute of
each process, and is not a global resource.  A child process inherits
the environment of its parent (at least if it's invoked from a shell
or via the system call execve), but it then owns a separate copy of
the environment in its own address space--you can change things all
day and it won't make any different to the parent process and any siblings
which might be around.

The only way to have a subprocess change the parent's environment
is to set up some form of convention to communicate this.  For
example, if you had a program which wrote out environment variable
assignments (in some shell's syntax) to the standard output, you
could invoke the command from the shell in the form:

eval `command`

The parent shell would be reading the shell variable reassignments
and changing them in its own context.  If your shell supported it
(csh, ksh), you could make this more convenient by using aliases.
-- 
Steve Dyer
dyer at harvard.harvard.edu
dyer at spdcc.COM aka {harvard,husc6,linus,ima,bbn,m2c,mipseast}!spdcc!dyer



More information about the Comp.unix.xenix mailing list