Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 29 Jun 2001 02:39:49 -0500
From:      Mike Meyer <mwm@mired.org>
To:        Tony Landells <ahl@austclear.com.au>
Cc:        questions@freebsd.org
Subject:   Re: How to have a script set an environment variable 
Message-ID:  <15164.12485.742513.46731@guru.mired.org>
In-Reply-To: <15208448@toto.iv>

next in thread | previous in thread | raw e-mail | index | archive | help
Tony Landells <ahl@austclear.com.au> types:
> I think that what Jim wants is a script that can "pass an environment
> variable back" to the invoking shell.
> 
> No matter what shell you choose, running a shell script (as such)
> creates a new shell for the duration of that script, which gets the
> environment variable and exits, taking the environment variable with
> it.
> 
> All shells support an alternative way of executing commands in a
> file which is usually called "sourcing" the file.  In sh/ksh/bash/...
> it is done by ". commands" (where "commands" is the file containing
> the commands to be executed); in csh/tcsh it is done with "source commands".
> 
> So the sequence (in csh or tcsh):
> 
> 	echo "setenv TERM xterm" > /tmp/term_set
> 	source /tmp/term_set
> 
> will set the TERM environment variable to "xterm".

It's normal to use "eval" for that kind of thing: eval `my_tset_script`

and then my_tset_scsript looks like:

	#!/bin/sh
	case $SHELL in
	    *csh) # use csh syntax
	   	echo "setenv VARIABLE=Foo"
		;;
	    *) # The default is the sh sentax...
	    	echo "export VARIABLE=Foo"
		;;
	esac

	<mike

--
Mike Meyer <mwm@mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?15164.12485.742513.46731>