[comp.unix.questions] csh setenv problem

saaf@joker.optics.rochester.edu (Lenny Saaf) (08/17/89)

I want to execute a csh script and set some variables for use in the
parent shell.  As I understand it, the script in the file is executed
in a subshell, so I think I have to "export" any csh variables I set
in the shell script.  That is, use setenv as opposed to set.  Well, I
can't seem to get it to work.  The shell script file looks like this:

# csh script
# filename is testscript
setenv FOO 'bar'
echo $FOO
# end

The result:

[1]% chmod +x testscript
[2]% testscript
bar
[3]% echo $FOO
FOO: Undefined variable.



What am I missing?

Related flame: HP's C shell documentation is lame.

--
* Len Saaf,  The Institute of Optics, University of Rochester,  Rochester, NY *
* Internet: saaf@joker.optics.rochester.edu  Bitnet: SAAF@UOROPT  Radio: NV2Z *
* Internet (last resort): saaf%joker.optics.rochester.edu@vm.cc.rochester.edu *

aland@infmx.UUCP (Dr. Scump) (08/17/89)

In article <SAAF.89Aug16153940@joker.optics.rochester.edu> saaf@joker.optics.rochester.edu (Lenny Saaf) writes:
>I want to execute a csh script and set some variables for use in the
>parent shell.  As I understand it, the script in the file is executed
>in a subshell, so I think I have to "export" any csh variables I set
>in the shell script.  That is, use setenv as opposed to set.  Well, I
>can't seem to get it to work.  The shell script file looks like this:

Let's see if I word this one a little more carefully so I don't get
flamed like last time:

This won't work because you are exporting to the shell spawned to run
your shell script -- when the script ends, that shell is gone.  You
can't run a shell script and have it effect the calling environment.

HOWEVER, you can process the commands in your *current* shell to get
the effect you want.  You can process commands from a file using
the "source" command (for csh) or the "." command (sh).

># csh script
># filename is testscript
>setenv FOO 'bar'
>echo $FOO
># end

>The result:
>[1]% chmod +x testscript
>[2]% testscript

   here, you should be saying "source testscript". execute permission
   is not necessary.  

>bar
>[3]% echo $FOO
>FOO: Undefined variable.
>What am I missing?
>* Len Saaf,  The Institute of Optics, University of Rochester,  Rochester, NY *

--
    Alan S. Denney  @  Informix Software, Inc.    
         {pyramid|uunet}!infmx!aland                 "I want to live!
   --------------------------------------------       as an honest man,
    Disclaimer:  These opinions are mine alone.       to get all I deserve
    If I am caught or killed, the secretary           and to give all I can."
    will disavow any knowledge of my actions.             - S. Vega

iwarner@zaphod.axion.bt.co.uk (Ivan Warner,G44 SSTF,6632,) (08/18/89)

From article <SAAF.89Aug16153940@joker.optics.rochester.edu>, by saaf@joker.optics.rochester.edu (Lenny Saaf):
> I want to execute a csh script and set some variables for use in the
> parent shell.  As I understand it, the script in the file is executed
> in a subshell, so I think I have to "export" any csh variables I set
> in the shell script.  That is, use setenv as opposed to set.  Well, I
> can't seem to get it to work.  The shell script file looks like this:
> 
> # csh script
> # filename is testscript
> setenv FOO 'bar'
> echo $FOO
> # end
> 
> The result:
> 
> [1]% chmod +x testscript
> [2]% testscript
> bar
> [3]% echo $FOO
> FOO: Undefined variable.
 
	Yes, the script is run in a sub-shell.

	But, anything set up using setenv is not passed back to the parent
shell either. As soon as the child shell exits, all environment variables are
thrown away. The environment variables are, however, passed down from parent
to child (set variables are not).

	The Bourne Shell (sh) has an export command, which exports
a variable to the parent shell. Csh does not have this.

	You have to 'source' the script file, i.e.

	source filename

	instead of just 'filename'. This runs the script in the current
shell (without spawning a sub-shell). Any setenvs/set/alias commands will
be in effect when this is done. You cannot have parameters to the script
file when using 'source' though (I think).


	Ivan Warner

jik@athena.mit.edu (Jonathan I. Kamens) (08/21/89)

In article <2312@zaphod.axion.bt.co.uk> iwarner@zaphod.axion.bt.co.uk writes:
>	The Bourne Shell (sh) has an export command, which exports
>a variable to the parent shell. Csh does not have this.

  In a word, wrong.

  There is no mechanism for passing environment variables from a child
process back to its parent.  It can't be done.  Unless you hack in
some form of interprocess communication between two processes whose
source code you write yourself, so that they can pass variables back
and forth along a pipe or something, and deal with them appropriately.

  Like csh, sh has two different kinds of variables, environment
variables and local shell variables.  When you set a variable with the
command "FOO=bar", you create a local variable with that value (if the
variable did not already exist), or change the value of the local
variable (if it already existed as a local variable), or change the
value of the environment variable (if it already existed as an
environment variable).  The command "export FOO" does NOT send the
value of FOO to the parent process of the shell, it changes FOO from a
local variable into an environment variable, so that child processes
will inherit the variable.

  Observe:

Script started on Sun Aug 20 15:35:18 1989
achates% /bin/sh
$ /bin/sh			# There are now two sh's running, and
				# one is the child of the other
$ FOO=bar; export FOO
$ echo $FOO
bar				# FOO is an environment variable in
				# the child sh
$ ^D$ echo $FOO			# ^D exits from the child shell
				# The line is blank, because FOO has
$ ^Dachates% exit		# no value in the parent sh
script done on Sun Aug 20 15:36:20 1989

Jonathan Kamens			              USnail:
MIT Project Athena				432 S. Rose Blvd.
jik@Athena.MIT.EDU				Akron, OH  44320
Office: 617-253-4261			      Home: 216-869-6432

guy@auspex.auspex.com (Guy Harris) (08/22/89)

>	The Bourne Shell (sh) has an export command, which exports
>a variable to the parent shell.

Wrongo.  The Bourne shell has an "export" command, which causes a shell
variable to be exported to *CHILD* shells.  It does *NOT* "export"
variables to the parent shell; a process can't just "export" an environment to
an already-existing process, it have to politely ask said process to
import said variable, using some protocol it and said process have
agreed on.  There is no "standard" protocol of that sort already
implemented by any shell I know of.