[comp.unix.xenix] Changing the environment in sh

paul@csnz.nz (Paul Gillingwater) (09/02/88)

I've Read The FM's, but I must be a bit dim - how do I change an
environment variable, e.g. PATH, using a /bin/sh script, and have
those changes effective in my login shell?

I tried setting the PATH and exporting it within the shell script,
and the environment for the shell that was executing the script
changed (as one would expect), but when that shell had finished,
my login shell environment was unchanged.

Basically I'm running two versions of the same software, so I put
them in different subdirectories, and I wanted to update my PATH 
and other shell variables to reflect that.  I tried creating two
versions of my .profile, V3.profile and V4.profile, then copying
the appropriate one over my .profile in my home directory, and then
did a . .profile in the shell script.  

What am I doing wrong?  If I do the . .profile from the keyboard,
it works fine; from a shell it forgets the PATH, even with an 
export PATH immediately following the . .profile.

Thanks for any help.....

-- 
Paul Gillingwater, Computer Sciences	Call this BBS - Magic Tower (24 hours)
paul@csnz.nz  (vuwcomp!dsiramd!csnz)	NZ +64 4 753 561 8N1 TowerNet software
P.O.Box 929, Wellington, NEW ZEALAND	V21/V23/V22/V22bis/Bell 103/Bell 212A
Vox: +64 4 846194, Fax: +64 4 843924	"All things must parse"-ancient proverb

dyer@spdcc.COM (Steve Dyer) (09/03/88)

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@harvard.harvard.edu
dyer@spdcc.COM aka {harvard,husc6,linus,ima,bbn,m2c,mipseast}!spdcc!dyer

daveh@marob.MASA.COM (Dave Hammond) (09/04/88)

In article <59@csnz.nz> paul@csnz.UUCP (Paul Gillingwater) writes:
>I've Read The FM's, but I must be a bit dim - how do I change an
>environment variable, e.g. PATH, using a /bin/sh script, and have
>those changes effective in my login shell?

Instead of having the shell execute the script, as in

$ sh script
(or)
$ script          # assuming script is chmod +x

have the shell evaluate, or source the script:

$ . script        # `dot script'

Dave Hammond
  UUCP: {uunet|...}!marob.masa.com!!daveh
DOMAIN: daveh@marob.masa.com
------------------------------------------------------------------------------

itkin@mrspoc.UUCP (Steven M. List) (09/04/88)

In article <59@csnz.nz> paul@csnz.UUCP (Paul Gillingwater) writes:
> I've Read The FM's, but I must be a bit dim - how do I change an
> environment variable, e.g. PATH, using a /bin/sh script, and have
> those changes effective in my login shell?
> 
You're not dim, just apparently a novice with the shell.  This is one of
the first difficult lessons of shell programming.  You cannot change the
value of a variable (environment or otherwise) from a subshell, since
the environment of a superior shell is not available to the subshell.
The only way to do this is with the "." (dot) command which can loosely
be understood to mean "read in the following file (using PATH if
necessary) and execute it as if I had typed the commands directly into
my keyboard".  Of course, if you include the dot command in a shell
script, it will execute the commands AS IF THEY WERE PART OF THE CALLING
SCRIPT.  There is NO OTHER WAY to change shell environment variables.
-- 
*  Steven List @ Transact Software, Inc.
*  {coherent,mips,ubvax}!mrspoc!itkin
*  Voice: (415) 961-6112

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (09/09/88)

You have to execute the original shell script with the current shell. If
you say
	setup1		# you get a new shell
while
	. setup1	# will allow env changes
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me