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 |
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 |
Quote:
Quote:
Quote:
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 |
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? |
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 |
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. |