Sunday, November 22, 2009

Environment Variables In Linux

Today I was installing Java Media Framework (JMF) for my project work. I have wasted half of the day to set Environment variables. then I successfully Installed, but JMF is not working as in windows. :( but I Explored more details about Environment Variables in Linux. :)

Environment variables handle or control the flow of running processes in the operating system. Environment variable has a value and a variable which is using some particular application. As a example in Java program, Operating system consist a variable call "PATH" and the value is the path of java's bin directory. Likewise most of the programs (actually running processes) are using Environment Variables for their various types of activities.

We can easily view already stored Environment Variables using Terminal.

  • Display Currently defined variable list.
  • Display Currently defined variable list.
echo $PATH (display the program search path)
  • using env
You can run a command in a modified version of the current environment (see man page) also it is using to display whole environment variables list. Just type env also It can use for delete or add a variable temporally.

  • Set Environment variables.
Ex. Set Java bin PATH
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin

  • Erasing Environment Variables.
You can do it easily giving null value
export PATH=
Using above commands you can handle environment variables temporally. If you want to handle Environment Variables Permanently you want to know how they are stored in your architecture.

Environment Variables are stored in two difference ways called Session Specific and System Specific.

1. Session Specific Variables.
These are mostly stored in the specific hidden files/scripts in your home directory. Specially we can identify 3 ways to store Environment Variables.

  • ~/.profile
  • ~/.bash_profile or ~/.bash_login
  • ~/.bashrc
  • ~/.bash_logout
2. System Specific Variables
These Variables are using whole System and affected all users/sessions in runing time.

  • /etc/profile
  • /etc/bash.bashrc
  • /etc/environment

Finally I'm goint to do a funny work with environment variables.

Just type a command in your terminal.
export PS1="\u is a fool$ "
cool ah? ;) want back?
export PS1="\u@\H > "
You can do any cool things with PS1 variable. anyway As I said earlier this work is not permanent. close terminal an open back, all settings are restore as earlier.