30

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.

41

You have to use source or eval 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:

  1. Prepare a file sourcefile containg a list of commands to source in the current shell:

    export JAVA_HOME=/cygdrive/c/dev/Java/jdk1.5.0_22
    export PATH=$JAVA_HOME/bin:$PATH
    

    and then source it

    source sourcefile
    

    note that there is no need for a sha-bang at the begin of the sourcefile, but it will work with it.

  2. Prepare a script evalfile.sh that prints the command to set the environment:

    #!/bin/sh
    echo "export JAVA_HOME=/cygdrive/c/dev/Java/jdk1.5.0_22"
    echo "export PATH=$JAVA_HOME/bin:$PATH"
    

    and then evaluate it:

    eval `evalfile.sh`
    
  3. Configure and run a new shell:

    #!/bin/sh
    export JAVA_HOME=/cygdrive/c/dev/Java/jdk1.5.0_22
    export PATH=$JAVA_HOME/bin:$PATH
    
    exec /bin/bash
    

    note that when you type exit in this shell, you will return to the father one.

  4. Put an alias in your ~/.bashrc:

    alias prepare_environ='export JAVA_HOME=/cygdrive/c/dev/Java/jdk1.5.0_22; export PATH=$JAVA_HOME/bin:$PATH;'
    

    and call it when needed:

    prepare_environ
    
  • 1
    I noticed that source works only for bash. Output for ksh: /bin/ksh: source: not found, for sh: sh: 0: source: not found. The answer below should be considered for these shells. – Danny Lo Apr 8 '15 at 9:19
  • @dannylo, you are right but the question is clearly bash centric. – andcoz Apr 8 '15 at 13:29
  • Is 'export' required with $PATH - which presumably exists already? – esskov Dec 27 '17 at 16:56
  • @esskov usually it isn't. I remember only an old old version of sh on AIX that required it (circa 1992) but, probably, it was some sort of bug. – andcoz Jan 17 '18 at 13:35
19

You could do that by using the source builtin:

. script_name

Some shells provide an alias named source:

source script_name
-1

installed Chocolatey @ unix, you can just run RefreshEnv to get updated environment variables into your current session

detail

  • 1
    The question that you linked too (and its answers) are about Windows.   This question is about Unix & Linux, so your post does not answer this question. – G-Man Says 'Reinstate Monica' Sep 7 '18 at 7:27
  • installed Chocolatey @ unix – 小色狼 Sep 8 '18 at 1:57
  • OK, you claim that this answer works for Unix.  Your detail link is still irrelevant.  Please provide details for how this works in Unix.  Links to authoritative documents would be good, but don’t post just links; put the relevant information into your answer. … Please do not respond in comments; edit your answer to make it clearer and more complete. – G-Man Says 'Reinstate Monica' Sep 8 '18 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.