Thursday, September 5, 2013

How to set Environment Variables in Unix / Linux Operating System ?


Setting Environment variables in Unix / Linux:

Here I show you how to set any environment variable in Linux OS.

First, we must know how many places are there to set environment variables in Unix.

I mean, 
 In windows we have two types 
     1) User Variables
     2) System Variables

        Here, user variables are only for that user. And system variables are for entire system and for all users exist in that Operating System.

Generally we know how to set these variables in windows, because it is GUI based setup.

But in Unix / Linux we can't do like that. Instead we have to place these variables in a shell script file. So that, those scripting statements are executed and those variables are available. 


Placing Unix Environment variables:

User Level:
                                In every user's Home directory we have some hidden files like             .bash_history.bash_logout.bashrc.profile 

           Choose any one file from .bashrc or .profile from above files.
and add variable setup statement at the end of that file along with the comment and save it.

Before going to use these environment variables, we must logout the account and login.

for example:
To set JAVA_HOME :
      #Setting JAVA_HOME variable
      export JAVA_HOME=~/jdk1.6xx

To set PATH for java's bin directory
     #Adding java to PATH variable
      export PATH=$PATH:$JAVA_HOME/bin


System Level:
                                 In every Unix System we have profile under the directory /etc.

/etc/profile - is a shell script file.

Here also, we have to add environment variable setup statement at the end of that file. To avail these environment variables, we must restart the system.

Checking of these Environment Variables:

      Use echo command to see whether the environment variable is set or not.

example:
       #echo $JAVA_HOME
    or 
      #echo $PATH


Note: 
In the csh shell: use - setenv PATH=$PATH:$JAVA_HOME/bin
In the bash shell (Linux): use - export PATH=$PATH:$JAVA_HOME/bin
In the sh or ksh shell: use - PATH=$PATH:$JAVA_HOME/bin





   

No comments:

Post a Comment