The PATH Environment Variable

Many commands you enter at the command line require the use of an external program that is loaded from the filesystem. For example, commands such as mkdir and wc actually reside in the /bin folder.

Whenever you enter an instruction that Bash doesn’t recognize, it tries executing it as a program and returns an error if no program of the same name is found. And this doesn’t only include the main commands we’ve been looking at, because you can run almost any program from the command line.

But with a filesystem comprising thousands of files, how does Ubuntu know which programs to run and from which directories? The answer is that it uses a system environment variable to point to a subset of folders it must search upon receipt of an unknown command. This variable is called PATH, and it can be displayed using the echo command, like this (the $ symbol is required):

echo $PATH

The result of issuing this command will look something like the following seven absolute folder paths, separated by colons:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Each time an unknown command is entered, Ubuntu will search each of the folders in the path in the order they are provided to try and find a program of the same name. If one is found, it is executed; otherwise, an error message is displayed.

These seven folders provide easy access to all the main programs in the operating system, including the games. But any programs that are not located in one of these folders cannot be executed by simply entering their names at the command line.

For example, let’s say you have downloaded a utility program into your home folder called diary. If you enter its name at the command line, you will receive an error message because it is not located in the path. Instead, you would enter a command such as the following to run it (remembering that the ~ symbol is shorthand for your home folder):

~/diary

Or, if you have saved it to a folder outside of your path, you would have to enter the absolute path and filename in order to run it. Of course, this assumes that diary is a simple standalone program that doesn’t require installation, because most major applications will place an executable program file somewhere in your path during the installation process.

Now let’s look at a possibly confusing situation by assuming you have entered cd ~ to set your home folder as your working directory, and have saved into it a program called find. In this case you might think you can now enter find and the program will run, but actually it won’t, because there’s a program with the name find located in the /usr/bin folder, and Ubuntu will execute that file by default because it is in the path, and that gets searched before the current folder.

One solution is to reference the program with either an absolute or a relative path and filename, such as this:

~/find

However, when a program is in the current directory, you can use the single period operator to run it, like this:

./find

This turns the command into a relative pathname and tells the system to run the program exactly where you say it is, not to first search for the program in the path.

Alternatively, if you tried to run a program in your home folder (or any other not in the path) that didn’t have a counterpart file with the same name somewhere in path, you would just receive a “command not found” error message, unless you properly identified the program’s location with a ~, a ., or another relative or absolute prefix.