Re: change environment variable via script

Top Page
Attachments:
Message as email
+ (text/plain)
+ (text/plain)
Delete this message
Reply to this message
Author: der.hans
Date:  
To: Main PLUG discussion list
Subject: Re: change environment variable via script
Am 23. Dec, 2005 schwätzte Alex Dean so:

> I'm using bash (on OSX). In ~/.bash_profile, I have a CVSROOT variable set
> so I can connect to 1 CVS server. I occasionally want to connect to another
> cvs server, so I wrote a shell script to set a new CVSROOT value for me. The
> script appears to set the value correctly, but it doesn't change my
> environment settings outside of the script. It seems like it's a global vs.
> local variable issue. (CVSROOT is set differently within the context of the
> script, but is the change is forgotten when the script exits.) I don't do
> shell scripts very often, so I'm probably missing something really obvious.
> How do I tell the script I want to change the value 'for real'?


Running a script or program execs a new shell from which to run the
program. That way the program can't break your current shell.

In order to have a script change environmental variables in the current
shell you need to source the script.

. ./myscript.sh

or

source ./myscript.sh

The former should work with all bourne shell implementations. The latter
works with bash and I think ksh.

For your particular problem you might also just use a command line switch
for cvs or an inline environmental variable assignment.

CVSROOT=":ext::/var/lib/cvs" cvs ...

cvs -d :ext::/var/lib/cvs ...

Double-check the syntax for cvs as the usage of that switch might not be
correct.

For bonus points figure out how to add an alias for cvstwo that will call
one of the those options to your profile :).

For extra bonus points figure out how to get that alias to use the
bash_completions function for cvs ;-).

ciao,

der.hans

> This is the script :
> #!/bin/sh
> echo "Selected server : "$1;
>
> case "$1" in
>  one)
>    CVSROOT=":ext::/var/lib/cvs"; export CVSROOT;
>  ;;
>  two)
>    CVSROOT=":ext::/var/lib/cvs"; export CVSROOT;
>  ;;
>  *)
>    echo '???';
>    exit 0;
> esac

>
> echo "CVSROOT value at end of script : "$CVSROOT;
> exit 0;
>
> And here's a session which -should- switch from server 'one' to server 'two',
> but it doesn't...
>
> sod:~/scripts alex$ echo $CVSROOT
> :ext::/var/lib/cvs
> sod:~/scripts alex$ ./cvsswitch two
> Selected server : 'two'
> CVSROOT value at end of script : ':ext::/var/lib/cvs'
> sod:~/scripts alex$ echo $CVSROOT
> :ext::/var/lib/cvs
> sod:~/scripts alex$
>
> You can see the script set the variable correctly, but the change didn't
> apply outside of the script. Why doesn't this work?!
>
> thanks,
> alex
> .
>
>
>
>


-- 
#  https://www.LuftHans.com/        http://www.CiscoLearning.org/
#  Join the League of Professional System Administrators! https://LOPSA.org/
#  Don't step in front of speeding cars, don't eat explosives
#  and don't use m$ LookOut :). - der.hans
---------------------------------------------------
PLUG-discuss mailing list -
To subscribe, unsubscribe, or to change you mail settings:
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss