LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Setting environment variables from shell script (https://www.linuxquestions.org/questions/linux-general-1/setting-environment-variables-from-shell-script-225802/)

theta 09-02-2004 01:57 PM

Setting environment variables from shell script
 
Hi all,

I have a usb flash jumpdrive that I use to carry all of my current projects. What I'm trying to do is write a shell script for it that sets up several environment variables that are needed by my development environemtn such as CVSROOT, Java dependencies, Editors, etc. The problem is that I can't seem to get the variables to export.

For example:

## Old $CVSROOT
eos% echo $CVSROOT
<MY_EMAILADDRESS>:/home/cvs/cvs
eos% echo $JALOPY_HOME
JALOPY_HOME: Undefined variable.

eos% sh envsetup.sh
Initializing Environment
Setting CVS Environment: CVSROOT=:local:/mnt/diskonkey/src
Setting Jalopy Home: /mnt/diskonkey/Apps/jalopy-ant-0.6.2/lib

## After running envsetup.sh
eos% echo $CVSROOT
<MY_EMAILADDRESS>:/home/cvs/cvs
eos% echo $JALOPY_HOME
JALOPY_HOME: Undefined variable.

As you can see its not changing. Here is the script:

#! /bin/sh
echo "Initializing Environment"
CVSROOT=:local:$PWD/src ; export CVSROOT ; echo "Setting CVS Environment: CVSROOT=${CVSROOT}"
JALOPY_HOME=$PWD/Apps/jalopy-ant-0.6.2/lib ; export JALOPY_HOME; echo "Setting Jalopy Home: ${JALOPY_HOME}"

What am I missing? I'm not doing anything different that what some of my start up scripts do so how is running them from a console window any different?

Thanks for any help,
Joel

mikshaw 09-02-2004 02:38 PM

You're running a script, then testing the variables from the current shell, yes?
When that script ends, the shell which ran the script also closes, and you're back to your old environment.
Any application which needs to use those variables needs to be spawned from the same shell which created the variables, or a child of that shell.

To test this, add the line "xterm &" at the end of your script.
Then test your environment from that xterm. You'll probably see the correct values.

Another option...
instead of executing the script (which opens a new shell), source it from your current shell:
source envsetup.sh
or
. envsetup.sh

theta 09-02-2004 03:02 PM

Quote:

You're running a script, then testing the variables from the current shell, yes?
Thats right.

Quote:

When that script ends, the shell which ran the script also closes, and you're back to your old environment.
Any application which needs to use those variables needs to be spawned from the same shell which created the variables, or a child of that shell.
Oh, that explains it.

Quote:

To test this, add the line "xterm &" at the end of your script.
Then test your environment from that xterm. You'll probably see the correct values.
No that didn't quite work the way I had hoped. When the xterm window pops up, I check it with something like: echo $CVSROOT and I get my old CVSROOT not the new one that the shell supposed to have set.

The only way I was able to get the new variables to take is if I open a terminal window, change my shell to sh then source the file like you suggested:

bash-2.05b$ sh
sh-2.05b$ source envsetup.sh
sh-2.05b$ echo $CVSROOT
:local:/mnt/diskonkey/src ## Correct new CVSROOT

This is fine but hoping there is a bit smoother solution. I like the idea of a new xterm window poping up ready to go. That way I can start the script by double clicking on the icon in a file explorer and a new window pops up with my environment ready to go.

Any ideas of things I might of missed?

Thanks
Joel

mikshaw 09-02-2004 04:15 PM

That's weird....I copied and pasted your script exactly as you have it here, added xterm, and the variables work fine for me.
You're sure you typed "echo $CVSROOT" in the new xterm window rather than the original shell window?

theta 09-02-2004 06:53 PM

Thats right I typed it in the XTERM window as opposed to the shell window. It is rather weird.

What I didn't mention before was that this is a unversity computer that I've been trying to get this to work on. Once I got home, it worked just like I hoped -- plugged in the usb stick, an icon popped up on my desktop, I double clicked on envsetup.sh, xterm came up, and all of my environment variables were initialized and ready to go. When I'm on campus again I'll check it and see if I can get it to work properly.

Joel:D

mikshaw 09-02-2004 08:50 PM

It's possible that the machine has bashrc setting some environment variables...i don't know if this would be an issue here.


All times are GMT -5. The time now is 08:58 AM.