How to find and set environment variables in LINUX

Linux comes with quite a few shells such as Bourne Shell, Bourne Again Shell, C Shell, Korn Shell, etc. The default shell for Red hat Linux is ‘ bash ‘ which is very popular since being the default, most users start by learning bash. I shall talk about the bash shell only in this article.
Typing the following at the shell
$ echo $SHELL
would give you the name of the current shell you are using. It would most probably be the bash shell in case you are a new user and have been assigned the default shell.
The bash shell is actually a program that is located at /bin/bash and is executed by Linux the moment a user successfully logs in after entering his user-pass. Once this shell starts, it takes over control and accepts all further user commands. The bash shell presents a $ prompt by default (for normal user accounts). You can change this prompt to whatever you like but leaving it at the default is best.
Shell Environment
All the programs that run under Linux are called as processes. Processes run continuously in Linux and you can kill or suspend different processes using various commands. When you start a program a new process is created. This process runs within what is called an environment. This particular environment would be having some characteristics which the program/process may interact with. Every program runs in its own environment. You can set parameters in this environment so that the running program can find desired values when it runs.
Setting a particular parameter is as simple as typing VARIABLE=value . This would set a parameter by the name VARIABLE with the value that you provide.
To see a list of the environment variables that are already set on your machine, type the following
$ envenv
This would produce a long list. Just go through the list before reading the next part of the article. Linux by default sets many environment variables for you. You can modify the values of most of these variables. A few of the variables that are set are
HOME=/home2/azhar
would set the home directory to /home/azhar. This is perfect in case your login name is azhar and you have been given a directory named /home/azhar . In case you don’t want this to be your home directory but some other one you could indicate so by typing the new directory name. The HOME directory is always the directory that you are put in when you login.
There are many advantages of using the HOME variable. You can always reach your home directory by only typing ‘ cd ‘ at the prompt, irrespective of which directory you are presently within. This would immediately transfer you to your HOME directory. Besides in case you write scripts that have $HOME present in them to refer to the current HOME directory, these scripts can be used by other users as well since $HOME in their case would refer to their home directories.
PATH=/usr:/bin/:usr/local/bin:.

This is a very important environment variable. This sets the path that the shell would be looking at when it has to execute any program. It would search in all the directories that are present in the above line. Remember that entries are separated by a ‘ : ‘ . You can add any number of directories to this list. The above 3 directories entered is just an example.
Note : The last entry in the PATH command is a ‘ . ‘ (period). This is an important addition that you could make in case it is not present on your system. The period indicates the current directory in Linux. That means whenever you type a command, Linux would search for that program in all the directories that are in its PATH. Since there is a period in the PATH, Linux would also look in the current directory for program by the name (the directory from where you execute a command). Thus whenever you execute a program which is present in the current directory you don’t have to type a ‘ ./programname ‘ . You can only type ‘ programname ‘ since the current directory is already in your PATH.
Remember that the PATH variable is a very important variable. In case you want to add some particular directory to your PATH variable and in case you try typing the following
PATH =/newdirectory
This would replace the current PATH value with the new value only. What you would want is to append the new directory to the existing PATH value. For that to happen you should type

PATH=$PATH:/newdirectory
This would add the new directory to the existing PATH value. Always a $VARIABLE is substituted with the current value of the variable.
SHELL=/bin/bash
This tells where the program that represents your shell is to be found. In case you typed /bin/ksh in the above, then your bash shell would be replaced with the ksh shell (korn shell). So in case you are not happy with the bash shell, you could replace the bash with some other shell.

To update an environment variable that lives until the next reboot:

    #export PATH=/usr/local/XXX/bin:$PATH
    #echo $PATH

Notice that colons are used to separate path items.

To create an environment variable that lives forever, update your .bash_profile file:

    XXXPATH=/usr/local/XXX/bin
    export XXXPATH
User Class Script to modify
One user $HOME/.bash_profile
All users except root /etc/profile
root /root/.bash_profile

1 thought on “How to find and set environment variables in LINUX”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.