Set parent env with csh script

John E Van Deusen III jiii at visdc.UUCP
Tue Jan 10 10:33:35 AEST 1989


You write:

> I would like to write a shell script (csh preferred) that will
> set a few environment variables to various things.
  
Suppose you have a file named envtest containing the following:

export TEST TEST1
TEST=xxx
TEST1="aaa bbb"

If envtest is executable, then preceding the name with a ". "
will execute envtest in the same shell.  For example:
$ . envtest

The shell command
$ eval `cat envtest`
will also put TEST=xxx and TEST1=aaa bbb into your environment.

The env(1) command lets you modify the environment.  If the file
envtest contained

TEST=xxx

then the command
$ exec env - `cat envtest` sh
will leave you in a shell where only TEST=xxx is in the environment.
Note that env(1) only wants arguments of the form name=value.  On my
system TEST1="aaa bbb" will not work with env, because it reevaluates
its arguments.

> Is it possible for a subshell to change the calling shell's 
> environment?  How?

You can use getenv(3C) from a C program to return the address of an
environment variable of the form name=value.  You can modify what is
stored at that location, but not with impunity.  If you set
TEST=xxxxxxxxxx
prior to invoking your program, then you can change its value to
be anything up to 10 characters long (plus a terminating null).  The
important thing is that the storage for TEST has to be allocated
prior to the execution of your program.
--
John E Van Deusen III, PO Box 9283, Boise, ID  83707, (208) 343-1865



More information about the Comp.unix.wizards mailing list