How to change PATH in shell script?


Eric Wilson

I have several projects that need to change the version of Java/Grails/Maven. I'm trying to fix this with some scripting that can make changes. E.g:

#!/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 doing this, it successfully changed the PATH in the context of the script, but then the script ended without any changes being done.

How can I run a script to change the PATH of the shell currently in use?

I am using Cygwin.

Andkerz

You have to generate a new shell using sourceor or .eval

When running a shell script, a new subshell is spawned . This subshell will execute script commands. The shell environment in which the parent thing is happening will remain unchanged in the subshell .

There are many different techniques to address this situation:

  1. Prepare a file source file containing a list of commands sourceto be executed in the current shell :

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

    then find it

    source sourcefile
    

    Note that no changes are required at the beginning of the source file , but it can be used.

  2. Prepare a script evalfile.sh that outputs the commands to set up 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 use evalit:

    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 enter exitthis shell, you will return to the parent one.

  4. Add an alias to your name :~/.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
    

Related


How to change PATH in shell script?

Eric Wilson I have several projects that need to change the version of Java/Grails/Maven. I'm trying to handle this with some scripts that can make changes. E.g: #!/bin/sh export JAVA_HOME=/cygdrive/c/dev/Java/jdk1.5.0_22 export PATH=$JAVA_HOME/bin:$PATH expo

How to change PATH in shell script?

Eric Wilson I have several projects that need to change the version of Java/Grails/Maven. I'm trying to handle this with some scripts that can make changes. E.g: #!/bin/sh export JAVA_HOME=/cygdrive/c/dev/Java/jdk1.5.0_22 export PATH=$JAVA_HOME/bin:$PATH expo

How to change PATH in shell script?

Eric Wilson I have several projects that need to change the version of Java/Grails/Maven. I'm trying to handle this with some scripts that can make changes. E.g: #!/bin/sh export JAVA_HOME=/cygdrive/c/dev/Java/jdk1.5.0_22 export PATH=$JAVA_HOME/bin:$PATH expo

How to add a path to a shell script

David 542 To get the file cwdin python I can do the following: import os cwd = os.getcwd() filename = 'file.txt' path = os.path.join(cwd, filename) How would I do the same in a shell script? Fred Dantini The current working directory can be obtained in bash

How to add a path to a shell script

David 542 To get the file cwdin python I can do the following: import os cwd = os.getcwd() filename = 'file.txt' path = os.path.join(cwd, filename) How would I do the same in a shell script? Fred Dantini The current working directory can be obtained in bash

How to change directory in shell script?

Miton Shrivaca I wrote the following script to access SSH: #!/usr/bin/expect -f spawn ssh 123456 -p 2222 expect "password:" send "foopassword\r" interact #!/usr/bin/expect -fOnly works for me now because we have to automatically enter the password too. After

How to retrieve the path of the current script in Fish Shell

Adam In bash, we can retrieve the path of the current script through a $0variable , so if any script's dependent resources are in the same directory as the script directory, it can be used even if we are not executing the script in the script directory. How ca

How to retrieve the path of the current script in Fish Shell

Adam In bash, we can retrieve the path of the current script through a $0variable , so if any script's dependent resources are in the same directory as the script directory, it can be used even if we are not executing the script in the script directory. How ca

How to get dynamic path in shell script?

Bogaviev I am writing a script that requires to get a dynamic folder path like this. somefolder/prefix.datetime_suffix/foldera/folderb The format of datetime is YYYYMMDDHHMMSS. The datetime part is changing. In the script I need to get the full path to folder

How to retrieve the path of the current script in Fish Shell

Adam In bash, we can retrieve the path of the current script through a $0variable , so if any script's dependent resources are in the same directory as the script directory, it can be used even if we are not executing the script in the script directory. How ca

How to retrieve the path of the current script in Fish Shell

Adam In bash, we can retrieve the path of the current script through a $0variable , so if any script's dependent resources are in the same directory as the script directory, it can be used even if we are not executing the script in the script directory. How ca

How to get dynamic path in shell script?

Bogaviev I am writing a script that requires to get a dynamic folder path like this. somefolder/prefix.datetime_suffix/foldera/folderb The format of datetime is YYYYMMDDHHMMSS. The datetime part is changing. In the script I need to get the full path to folder

How to change PATH for Makefile $(shell...) command?

user 541686 when i run export PATH := mypath $(error $(shell echo "$${PATH}")) Nothing seems to PATHchange for my call shell. Why is this happening and how do I actually change the PATHfor shellcall? Florian Weimer Is this GNU make? There's a long-standing GN

How to change PATH for Makefile $(shell...) command?

user 541686 when i run export PATH := mypath $(error $(shell echo "$${PATH}")) Nothing seems to PATHchange for my call shell. Why is this happening and how do I actually change the PATHfor shellcall? Florian Weimer Is this GNU make? There's a long-standing GN

change shell in shell script

username In the default shell, the for loop given below for ((i=$llimit; i<=$ulimit; i++)); do echo $i done; It will throw error "'((' is not expected" but when switching to bash shell for loop works fine Is there a way to change the shell in shellscript

Change path address in text file via shell script

h747 In my Bash script I have to change the name to a path address (new address) in a text file: (MYADDREES) to ( /home/run1/c1) and save it as a new file. I did this: define a new variable = new address and try to replace it with the previous address in the t

Change path address in text file via shell script

h747 In my Bash script I have to change the name to a path address (new address) in a text file: (MYADDREES) to ( /home/run1/c1) and save it as a new file. I did this: define a new variable = new address and try to replace it with the previous address in the t

How to change text string using shell script

tibia /home/temp.txt http://url_new_address.com /home/list.txt URL http://url_address.com:URL1 URL http://url_address.com:URL2 URL http://url_address.com:URL3 How to replace line in temp.txt with URL2 in list.txt using shell script? after the command /home/l

How to change directory using shell script?

Sujatha I am trying to change directory to home/developer. I have used cd home/developerin shell scripts . After executing the script, it went back to the original directory where I executed the shell script. Radu Rădeanu When you start the script, a new proce

How to change the working directory of a shell script

Randy I have a Python script that finds files in relative directories. For example: Python scripts are located in /home/username/projectname/. I have a file which is called in a Python script in /home/username/projectname/subfolder. If I run the script from th

How to change text string using shell script

tibia /home/temp.txt http://url_new_address.com /home/list.txt URL http://url_address.com:URL1 URL http://url_address.com:URL2 URL http://url_address.com:URL3 How to replace line in temp.txt with URL2 in list.txt using shell script? after the command /home/l

How to change directory using shell script?

Sujatha I am trying to change directory to home/developer. I have used cd home/developerin shell scripts . After executing the script, it went back to the original directory where I executed the shell script. Radu Rădeanu When you start the script, a new proce

How to change date format in file in shell script?

Paul Dirac I have a file with dates in the format YYYY-MM-DD. I want to write a shell script to change the date format to DD/MM/YYYY. This is my first attempt to no avail #!/bin/bash NR=$(cat $1 | grep -o '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]' | cut -d'-

How to change the working directory of a shell script

Randy I have a Python script that finds files in relative directories. For example: Python scripts are located in /home/username/projectname/. I have a file which is called in a Python script in /home/username/projectname/subfolder. If I run the script from th

Change shell in script function

Will Niles I'm using ZSH as my main shell, but in my server .zshrcI'd like to set up an ssh command with Expect to make it easier to ssh into my dev box when building flash (doesn't really need any Security, it's all on the intranet) of sorts). I can pass the

How to add directories from Fish Shell PATH using Python script?

Justin Mayer From a Python-based install script running on Fish shell and supporting Python 2.7 and 3.4+, I would like to add the directory to an PATHenvironment variable if it doesn't already exist. After the script exits, it PATHshould contain the new direct

How to add directories from Fish Shell PATH using Python script?

Justin Mayer From a Python based install script running on Fish shell and supporting Python 2.7 and 3.4+, I would like to add the directory to an PATHenvironment variable if it doesn't already exist. After the script exits, it PATHshould contain the new direct