Understanding and Setting the PATH
UNIX uses the term “path” for two related concepts:
- The location of a file or directory on a hard drive. These are called File Paths, or more broadly a URI (yes, it is directly related to a URL on the Internet).
- The directories that should be scanned for executable code when you (or your desktop) enters a command.
The second definition is defined by a systemwide variable called, simply, PATH
. You can check your current PATH
setting:
$ echo $PATH /usr/pkg/bin:/usr/local/bin:/usr/bin:/bin:/usr/pkg/games:/home/klaatu/bin
What This Means to You:
When you type in a command, the computer knows it is supposed to attempt to execute the command. But how does the computer know what the command represents? Inherently, it does not. I n a much less convenient world, you “should” have to go to the directory where the command is located, and point specifically to it, and tell the computer to execute the code contained in the file:
$ cd /bin $ ./ls
Of course that's not really possible, since in order to get to a directory containing commands, you would need to use a command. Also, it would be inconvenient, so there is the PATH
variable, which your computer keeps in memory so that when you issue a command, like ls
or aj-snapshot
or synfigstudio
, it scans a defined set of directories to find out if such a command exists. If so, it executes the first such command it finds.
Create a test shell script and place it in ~/bin
. Create a ~/bin
directory if you don't already have one.
$ echo '#\!/bin/bash' >> ~/bin/foo $ echo 'echo "I told you it would work." ' >> ~/bin/foo $ chmod +x ~/bin/foo
Now try some commands:
$ cd ~/audio/fatChanceLester $ ls napalmLounge.tar.gz fantasticUniverse.tar.gz lastChanceFester,tar.gz $ foo foo: Command not found.
In this example, the commands cd
and ls
were valid, but the nonsense command foo
did not appear in any of the directories in PATH
, so it returned an error.
Setting the PATH
A Linux system comes with a default PATH already set. It contains important standard directories like /bin
and /usr/bin
and /usr/local/bin
and a few others. This is enough to get you by just fine on an out-of-the-box Linux system, because those directories are where all the standard applications have been placed.
If you install major extra software, like Java, then the installer will modify your path (you give it permission to do this when you install the application as root) so that when you attempt to launch a Java application, it knows where to find Java.
$ echo $PATH /bin:/usr/bin:/usr/local/bin:/usr/lib64/java/jre/bin
But what if you want to add things to your PATH manually? For example, there are several applications that are easiest to install either to /opt
or to ~/bin
, so you should be able to conveniently execute applications in those paths.
Setting a path is easy. There are two methods: one is temporary and one is persistent. Assuming you are intested in the persistent solution, open your ~/.bash_profile
in your favourite editor (if it does not exist, it's OK to create it).
Add your additional path settings to the current default systemwide PATH:
export PATH="${PATH}:/home/klaatu/bin"
This line simply takes whatever exists in the system PATH variable and appends /home/klaatu/bin
to it, and then exports that value into your environment.
Save the file and log out, and then back in. If you are too busy to log out, you can load the new PATH by re-loading your profile settings:
$ source ~/.bash_profile
Now your computer scans your home directory's bin
directory for any commands that it cannot first find in any of the default path locations.
$ foo I told you it would work.