Setting Shell Environment Variables | BASH Shell

June 4, 2011 / How-to Guide

Steps for setting Shell Environment Variables (bash shell)

In a Linux environment, the command line interpreter is referred as shell. Its basic role is of interpreting what is entered in the command line and offering an output upon execution of your command. In short, the program that holds the capability of understanding that is entered is known as the Shell.

There aren’t many shells available with Linux, what currently exists are Bourne Shell, Bourne Again Shell, C Shell, Korn Shell, etc.  while the standard shell used for Redhat Linux is ‘ bash ‘.
Webmasters who have used the command line on Windows can relate bash with Linux shell.

The below command is used to know the name of the shell that is being used by you :

$ echo $SHELL

More often it would be a bash shell if you are assigned the default shell by the web hosting UK company.
This particular program is situated at /bin/bash which gets executed by Linux instantly as a user is able to successfully log-in. The $ prompt is a default function of bash shell which can even be changed according to preferences. This prompt tends to vary with the type of shell used.

How to change the default prompt in Shell ?

Shell Environment

The programs used in Linux are termed as processes. They run on a continuous basis in Linux which can even be killed or suspended as per requirements. Everytime a program is started, a new process is initiated which are run within the Linux environment. Based on the preferences and requirements, users can set parameters in this environment so that the running program can find desired values when it runs.

Users can easily set a parameter by entering VARIABLE=value . This sets a parameter by the name VARIABLE with the value that has been provided by the user.

The below command can be entered to check the list of environment variables that are set on the machine :

$ env

Upon execution of this command, the screen would get populated with long list. As a default feature, Linux would by its own sets multiple environment variables for users. They can be modified by changing the values for majority of these variables. A few of the variables which are set are :

HOME=/home/stan

this sets the home directory to /home/stan. This is when you login name as stan and a directory named stan is assigned to you. There may be a possibility where you do not wish to be the home directory but some other, then you type the new directory name and proceed. The HOME directory is the default directory that gets assigned upon login.

Various benefits of using HOME variable exists, by using the ‘cd‘ command, no matter which directory you are in, you can instantly reach the home directory with it.

Another important environment variable in Linux is : “PATH

PATH=/usr:/bin/:usr/local/bin:.

This command allows you with setting the path which the shell looks at the time of executing a program. Each directory in the above command would get searched by shell. Administrators must make a note that the entries are separated by ‘ : ‘ . There isn’t any limit to adding number of directories to the list. Also, it must be noted that the last entry in the PATH command is a ‘ . ‘ (period), which indicates that it is the current directory in Linux. Everytime a command is entered, Linux searches for the program in every directory stated in its PATH. Since a period is included in the PATH, the current directory for program by the name is looked by Linux. This offers you a scope that whenever a program is executed by you which is present in the current directory (for example: a certain script that has been written on your own) you are not required to enter ‘ ./programname ‘ .  Simply entering the ‘ program name ‘ would be sufficient as the current directory is already in your PATH.

NOTE : The file must have execute permissions i.e. [755]

If you are required to add a particular directory to the PATH variable, you can use the below command:

PATH =/newdirectory

Note that this would only replace the current PATH value with a new one. You are required to append the new directory to the existing PATH value. You must use the following command for that purpose :

PATH=$PATH:/newdirectory

This adds the new directory to the existing PATH value. Always a $VARIABLE is replaced with the current value of the variable.

PS1=boss

PS1 is another shell prompt that can be used. This defines how your shell prompt must look like. As stated earlier, the default one is ‘ $ ‘ in bash shell. Whereas in the above scenario, ‘$’ would be replaced ‘boss’, ie,  an ls command would look something like

boss> ls

SHELL=/bin/bash

This tells where the program that represents your shell is to be found. If you enter /bin/ksh , the bash shell is replaced with the ksh shell ie. korn shell.

NOTE : If you intend to make the above changes permanent, it is required to make changes to the .profile file which can be found in HOME directory. You must type the required commands on separate lines.

Spread the love