Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have one common function called SetEnv in a file called SetEnv.

File SetEnv:

SetEnv()
{    
export PATH=$PATH:$JAVA_HOME/bin
}

File Script.sh:

SetEnv

echo $PATH

While running ./Script.sh, I cannot see the updated path.

Please help how to solve this.

share|improve this question
    
That is not how you set an environmental variable. help.ubuntu.com/community/EnvironmentVariables –  bodhi.zazen Oct 8 '13 at 19:05

1 Answer 1

That's because when you run a script from terminal, this doesn't run in the current shell terminal, but in a subshell. The variables, functions and aliases created in this subshell are only known to the particular bash session of that subshell. When that shell exits and the parent regains control, everything is cleaned up and all changes to the state of the shell made by the script, are forgotten.

To prevent this, you can source the script in the current shell terminal, using the following command:

source Script.sh

or, simple:

. Script.sh          #dot, space and the path to the script

Also, your Script.sh should look like:

#!/bin/bash

source /path/to/SetEnv  #if you want that your script to know about 'SetEnv' function.

SetEnv

echo $PATH

More about: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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