Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 29 Jun 2001 15:46:00 +1000
From:      Tony Landells <ahl@austclear.com.au>
To:        "Ian P. Thomas" <ipthomas_77@yahoo.com>
Cc:        jim@freeze.org (Jim Freeze), freebsd-questions@FreeBSD.ORG
Subject:   Re: How to have a script set an environment variable 
Message-ID:  <200106290546.PAA16428@tungsten.austclear.com.au>
In-Reply-To: Message from "Ian P. Thomas" <ipthomas_77@yahoo.com>  of "Thu, 28 Jun 2001 23:56:19 -0400." <200106290356.XAA04190@scraemondaemon.my.domain> 

next in thread | previous in thread | raw e-mail | index | archive | help
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".

Of course, the problem with this is that if the "script" is long and
sets lots of variables, they finish up in your shell as well.

The alternative is to have the script echo the command you want as
it's (only) output, and then run that:

	echo "echo setenv TERM xterm" > /tmp/term_set
	chmod +x /tmp/term_set
	eval `/tmp/term_set`

which will also set TERM to xterm.

There's always a choice ;-)

Tony
-- 
Tony Landells					<ahl@austclear.com.au>
Senior Network Engineer				Ph:  +61 3 9677 9319
Australian Clearing Services Pty Ltd		Fax: +61 3 9677 9355
Level 4, Rialto North Tower
525 Collins Street
Melbourne VIC 3000
Australia



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?200106290546.PAA16428>