There are two ways to setup environment variables in linux systems ( Ubuntu , Red Hat , Fedora etc)
- Using the export method of adding environment variables to linux . However it is only temporary solution , it will make variables available only for current session.
- Editing .bash_profile file and adding it there permanently
Using export
Consider the example you want to add JAVA_HOME variable for java installation at /usr/java/
Using export we would type command like
# export JAVA_HOME=/usr/java
To add it to our PATH variable we would type
# export PATH=$PATH:$JAVA_HOME/bin
The above command says add the bin directory in JAVA_HOME to the PATH variable
However using export would not be permanent and would be gone when your session is over
Using bash_profile
Assuming that you are using default bash shell ( See note in end )
To add environment variable permanently we have to edit the bash_profile file.
Move to your home directory (e.g /home/yourname and type )
# vi .bash_profile
The above command may ask for root user rights , so in that case you have to type like
# sudo vi .bash_profile
The full stop (.) before the file name makes it a hidden file
Type in the end of the file any path variable you want to add
e.g
JAVA_HOME=/usr/java/jdk1.6.0_26
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH
type i to enter the insert mode
then add whatever you want to the end of it. Press the escape key to exit the INSERT mode.
Type :w to write the file
Type q to quit the vi editor
That’s it. The path variable has been stored permanently in the Linux system
--------------------------
NOTE:
Depending upon the shell you are using , you need to edit .profile or .bash_profile file.
To find out type echo $SHELL
If you are using ksh,
Edit ~/.profile to add any evn. variable.
e.g.
MY_LIBS=/home/my/libs
export MY_LIBS.
If you are using bash,
Edit ~/.bash_profile to add the env.
To find out what shell you are usin, do
echo $SHELL or
cat /etc/passwd | grep `id -un`