[NBLUG/talk] bash script wonder

Mitch Patenaude mrp at sonic.net
Tue Oct 5 01:49:05 PDT 2004


On Oct 4, 2004, at 11:56 PM, Micxz wrote:
> When I run it I get:
> micxz at neptune:~> ./test.sh
> SuSE. I think
> made it
> /opt/kde3
>
> OK. Looks like worked right. both conditions are true. But I don't 
> know why after the script exits I get:
>
> micxz at neptune:~> env | grep KDEDIR
> micxz at neptune:~> echo $KDEDIR
>
> nada' Were did my environment var go? Thanks in advance'

This is a common mistake when writing shellscripts.  The script runs in 
a new instance of the shell which is a child of the interactive shell, 
and therefore the changes made to the environment don't propagate back 
up to the parent.  If you want the script to run in the current shell, 
then invoke it like so:

foo at bar:~> . ./test.sh

Or less cryptically:

foo at bar:~> source test.sh

Of course, if your intent is not to modify the environment of the 
currently running interactive shell, but only to set it up for this 
script and processes spawned by the script, then the technique you're 
using will work fine.

   -- Mitch





More information about the talk mailing list