Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!auspex!guy From: guy@auspex.UUCP (Guy Harris) Newsgroups: comp.unix.questions Subject: Re: Manipulating the Shell environment variables Message-ID: <1200@auspex.UUCP> Date: 24 Mar 89 20:23:30 GMT References: <7977@chinet.chi.il.us> Reply-To: guy@auspex.UUCP (Guy Harris) Distribution: usa Organization: Auspex Systems, Santa Clara Lines: 25 >How can I change variables in the environment in a C program, >and have the change remain in effect when I exit it? > >putenv() does not make the change permanent; the variable is >it's original value when the C program exits. The root of the problem is the incorrect use of "the" in the phrase "the environment". There is no single environment that can be referred to as "*the* environment"; every process has its own environment. A process can modify its environment; the modifications will be propagated to any processes started after the modification is made (because the environment is, in all UNIX implementations I know of, part of the process's address space, and is duplicated when a "fork" is done; when an "execv" or "execl" is done, the current environment is passed across the "exec"). The word "copy" is important here; if a process modifies *its* environment, it has *no* effect on the environment of other processes. I presume the environment you want to change is that of the shell from which you're running the C program in question; the only way to do that is to politely ask that shell to change its environment. You can, for example, have your program cough up a bunch of environment variable settings to its standard output, and have the shell pick them up and execute those settings; this is, for example, how the "tset" command, from BSD, is used to set TERM and TERMCAP.