Working with Environment Variables in Linux

  1. How to set PATH environment variable?
  2. How to set JAVA_HOME environment variable?
  3. How to set Java bin directory to PATH?
  4. How to set JBOSS_HOME environment variable? (If you are using JBoss Appliation Server)
  5. How to set ANT_HOME environment variable and its bin to PATH? (If you are using Ant build tool)

If you want to set these variable globally. Then you need to make entry for these variables in the file /etc/profile.

Otherwise, for a specific user: In your home directory* of Linux you have a file ".bash_profile". So you need to modify this file. As this is a hidden file, so you will not find this by typing ls command. To see it use -ls option. E.g.

Now to modify this file use the vim Editor (You should know how to use the vi editor in Linux.). E.g.

Now suppose you have

Java Home directory: /home/ranjan/installed/jdk1.5.0_12
Java bin directory: /home/ranjan/installed/jdk1.5.0_12/bin
Ant Home directory: /home/ranjan/installed/apache-ant-1.7.0
Ant bin directory: /home/ranjan/installed/apache-ant-1.7.0/bin
JBoss Home directory: /home/ranjan/installed/jboss-eap-4.2/jboss-as

So the sample .bash_profile file is:

Similarly you can do for /etc/profile(if you are willing to set these variable globally).

Now this is all done. Now logout and login again. Now verify all the environment variables using echo command. e.g.

** Home Directory: When you first login to any Linux system, by default you are in you home directory first, e.g. if you login with user name suppose “ranjan”, your home directory is: /home/ranjan. Your home directory can always check by the $HOME environment variable. So to check it, you can use the echo command in the following way:

Note:

Some flavour of Linux may not have .bash_profile file. It may have file like .profile or something else. Modifying /etc/profile instead of .bash_profile has an advantage. In this case you do not need to install Java separately for each user. There is a single installation of java in /usr/java/ directory, and single entry for the environment variables in /etc/profile for all the users.

Leave a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

Fork me on GitHub