Wednesday, November 26, 2014

Append to $PATH vs import $PATH

We know .bashrc file inside our Ubuntu home folder is the file in which we set the environment variables for a single user. This is the bash script file.

Linux determines the executable search path with the $PATH environment variable

For example, after installing JDK we set $JAVA_HOME variable and update the $PATH variable.

To set those variables, we add the following lines at the end of the .bashrc file.

JAVA_HOME=/home/user/Java/jdk1.7.0_03
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH

So, what is the actual use of export even after appending the relevant path to the $PATH environment variable? Why do we need to export even after appending it to the $PATH variable?

The reason is,

Appending only to the $PATH is not sufficient because, it is same as setting a value inside a script (Here, only in .bashrc script). Thus that change is effective only within the script. In Ubuntu, it will be effective only within bash.

To make this change effective to any program called by bash we need to export the environment variable.


No comments: