1. Computing & Technology

Introduction to Linux

From Machtelt Garrels

3.2.1. The path

When you want the system to execute a command, you almost never have to give the full path to that command. For example, we know that the ls command is in the /bin directory (check with which -a ls ), yet we don't have to enter the command /bin/ls for the computer to list the content of the current directory.

The PATH environment variable takes care of this. This variable lists those directories in the system where executable files can be found, and thus saves the user a lot of typing and memorizing locations of commands. So the path naturally contains a lot of directories containing bin somewhere in their names, as the user below demonstrates. The echo command is used to display the content ("$" ) of the variable PATH :


   

 
rogier:> echo $PATH
 /opt/local/bin:/usr/X11R6/bin:/usr/bin:/usr/sbin/:/bin
 

In this example, the directories /opt/local/bin , /usr/X11R6/bin , /usr/bin , /usr/sbin and /bin are subsequently searched for the required program. As soon as a match is found, the search is stopped, even if not every directory in the path has been searched. This can lead to strange situations. In the first example below, the user knows there is a program called sendsms to send an SMS message, and another user on the same system can use it, but she can't. The difference is in the configuration of the PATH variable:


   

 
[jenny@blob jenny]$ sendsms
 bash: sendsms: command not found
 [jenny@blob jenny]$ echo $PATH
 /bin:/usr/bin:/usr/bin/X11:/usr/X11R6/bin:/home/jenny/bin
 [jenny@blob jenny]$ su - tony
 Password:
 tony:~>which sendsms
 sendsms is /usr/local/bin/sendsms
 
 tony:~>echo $PATH
 /home/tony/bin.Linux:/home/tony/bin:/usr/local/bin:/usr/local/sbin:\
 /usr/X11R6/bin:/usr/bin:/usr/sbin:/bin:/sbin
 

Note the use of the su (switch user) facility, which allows you to run a shell in the environment of another user, on the condition that you know the user's password.

A backslash indicates the continuation of a line on the next, without an Enter separating one line from the other.

In the next example, a user wants to call on the wc (word count) command to check the number of lines in a file, but nothing happens and he has to break off his action using the Ctrl +C combination:


   

 
jumper:~> wc -l test
 
 (Ctrl-C)
 jumper:~> which wc
 wc is hashed (/home/jumper/bin/wc)
 
 jumper:~> echo $PATH
 /home/jumper/bin:/usr/local/bin:/usr/local/sbin:/usr/X11R6/bin:\
 /usr/bin:/usr/sbin:/bin:/sbin
 

The use of the which command shows us that this user has a bin -directory in his home directory, containing a program that is also called wc . Since the program in his home directory is found first when searching the paths upon a call for wc , this "home-made" program is executed, with input it probably doesn't understand, so we have to stop it. To resolve this problem there are several ways (there are always several ways to solve a problem in UNIX/Linux): one answer could be to rename the user's wc program, or the user can give the full path to the exact command he wants, which can be found by using the -a to the which command:


   

 
jumper:~> /usr/bin/wc -l test
 10 test
 

If the user uses programs in the other directories more frequently, he can change his path to look in his own directories last:


   

 
jumper:~> export PATH=/usr/local/bin:/usr/local/sbin:/usr/X11R6/bin:\
 /usr/bin:/usr/sbin:/bin:/sbin:/home/jumper/bin
 


       Changes are not permanent!
        

Note that when using the export command in a shell,

* License

* Introduction to Linux Index

©2012 About.com. All rights reserved.

A part of The New York Times Company.