I have several projects that require me to change versions of Java/Grails/Maven. I'm trying to handle this with some scripts that would make the changes. For example:
#!/bin/sh
export JAVA_HOME=/cygdrive/c/dev/Java/jdk1.5.0_22
export PATH=$JAVA_HOME/bin:$PATH
export GRAILS_HOME=/cygdrive/c/dev/grails-1.0.3
export PATH=$GRAILS_HOME/bin:$PATH
export MAVEN_HOME=/cygdrive/c/dev/apache-maven-2.0.11
export PATH=$MAVEN_HOME/bin:$PATH
which java
which grails
which mvn
When this executes, it successfully changes the PATH within the context of the script, but then the script ends, and no change has been accomplished.
How can I run a script in a way to change the PATH in for the shell in which I am currently working?
I'm using Cygwin.
Best Answer
You have to use
source
oreval
or to spawn a new shell.When you run a shell script a new child shell is spawned. This child shell will execute the script commands. The father shell environment will remain untouched by anything happens in the child shell.
There are a lot of different techniques to manage this situation:
Prepare a file sourcefile containg a list of commands to
source
in the current shell:and then source it
note that there is no need for a sha-bang at the begin of the sourcefile, but it will work with it.
Prepare a script evalfile.sh that prints the command to set the environment:
and then
eval
uate it:Configure and run a new shell:
note that when you type
exit
in this shell, you will return to the father one.Put an alias in your
~/.bashrc
:and call it when needed: