Home Operating system The PATH Variable in UNIX

The PATH Variable in UNIX

2459 17/08/2021

The PATH Variable

After reading this document, you should know what the PATH variableis, how to set it, and how to view the directories currently therein.

Motivation

Consider the following UNIX terminal session:

dbettis@rhino[~]$ ls...

When you type the command

ls

, the shell dutifullyexecutes the command and returns the results to you. During thecourse of a terminal session, you type more commands. These includethings like

emacs

,

firefox

, so on and soforth. But where do these commands come from? Obviously, they'reincluded when you install the operating system, but where are they?

The UNIX command

which

will tell you the full path to abinary it's about to execute. For example:

dbettis@rhino[~]$ which ls/bin/ls

That means that the exectuable for the command

ls

is locatedin

/bin

. Alternatively, to run

ls

, you can type in thefull path to the command:

dbettis@rhino[~]$ /bin/ls...

It seems like there's a bit of magic going on here, however. How doesthe system know that

ls

is in

/bin

? The waythe system knows is the

PATH

environment variable!

Envrionment Variables

First, what's an environment variable? It's a variable that persistsfor the life of a terminal session. Applications running in thatsession access these variables when they need information about theuser. To see a listing of all the environment variables, execute thefollowing:

dbettis@rhino[~]$ exportdeclare -x USER="dbettis"...

The name of variable is

USER

and the contents of thatvariable is

dbettis

. Another way to see the contentsof an environment variable is to do the following:

dbettis@rhino[~]$ echo $USERdbettis

The PATH Environment Variable

The PATH environment variable has a special format. Let's see what itlooks like:

dbettis@rhino[~]$ echo $PATH/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:.

It's essentially a

:

-separated list of directories. Whenyou execute a command, the shell

searches through each of thesedirectories, one by one

, until it finds a directory where theexecutable exists. Remember that we found

ls

in

/bin

, right?

/bin

is the second item in thePATH variable. So let's remove

/bin

from

PATH

. We can do this by using the

expo

rt

command:

dbettis@rhino[~]$ export PATH=/usr/local/bin:/usr/bin:/sbin:/usr/sbin:.

Make sure that the variable is set correctly:

dbettis@rhino[~]$ echo $PATH/usr/local/bin:/usr/bin:/sbin:/usr/sbin:.

Now, if we try to run

ls

, the shell no longer knows tolook in

/bin

!

dbettis@rhino[~]$ ls-bash: ls: command not found

As expected,

ls

can no longer be found. Let's add

/bin

back to

PATH

, as

ls

is avery useful thing to have.

dbettis@rhino[~]$ export PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:.

Adding to PATH

There are many times where you'll want to append an item to

PATH

.First, let's see what the current

PATH

is:

dbettis@rhino[~]$ echo $PATH/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:.

The way to

add

a directory is as follows:

dbettis@rhino[~]$ export PATH=$PATH:/new/path

This command adds

/new/path

to

PATH

. Let's see if it got updated:

dbettis@rhino[~]$ echo $PATH/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:.:/new/path

Making this happen every time you login

There's a special file in your home directory called

.bashrc

In UNIX, a convention is that files beginningwith

.

are configuration files, and thus should be hiddenfrom view.

ls

will only list files beginning with a

.

if passed the

-a

flag. e.g.

dbettis@rhino[~]$ ls -a

At any rate, this file (.bashrc), simply contains a list of commands.Each one of these commands gets executed every time you create a newshell.

dbettis@rhino[~]$ cat .bashrcexport PATH="$PATH:/p/firefox/bin"..

Every time a shell is started,

/p/firefox/bin

is addedto

PATH

. If you wish to have certain directoriesautomatically added to

PATH

, simply place those commandsat the end of this file. Log out and log back in to view the changes.Alternatively, you can load the contents of that file in the currentsession:

dbettis@rhino[~]$ . .bashrc

Latest: Norton 360 Platinum 2021 – Antivirus software for 20 Devices with Auto Renewal - 3 Months FREE - Includes VPN, PC Cloud Backup & Dark Web Monitoring [Download]
Next: How to Update USB Drivers on Windows 10 (Automatic and Manual)
Popular Articles/span>
Back to top