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 ...

Get Ubuntu: Up and Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.