In the Linux operating system the $PATH is a listing of all directories where the system will look for commands. What this means is that all of the commands located in the directories included in your path will be globally executable. For example: The /usr/bin directory contains quite a lot of commands that can be excuted from within any directory on your system. Because of this you can issue the ls command from within any directory and get the listing of the contents of that directory. If the ls command wasn't in a directory in your path you would have to include the explicite path to that command (i.e. /usr/bin/ls).
As a Linux user you can add directories to your $PATH. This is helpful when you don't want to add a command to a directory in your $PATH but you want that command to be globally executable. Doing this is actually quite easy.
What is currently in your $PATH?
NOTE: This article applies only when you are using the Bash shell. To find out what directories are included in your current $PATH issue the command:
You should see something like:
Notice the /opt directory is missing. Often the /opt directory is a great place to "install" other applications for global use. But if this directory is not in your $PATH, you will always have to use the explicit path to call a command. With that in mind let's add /opt.
In order to add a directory you have to edit a file in your ~/ (home) directory. The .bash_profile file determines user specific environment and start up programs. This file also checks for a .bashrc file for aliases and functions, but that has nothing to do with your $PATH.
There is one particular line you need to examine in your .bash_profile:
This is the line that determines anything extra in your $PATH. As you can see, in the example above, the extra directory added to the users' $PATH is the ~/bin directory. Of course in most distributions this isn't used (or even created during installation). Why ~/bin is still included I do not know. In order to add another directory to your $PATH in this line you would seperate the directories with a ":". To add the /opt directory that line would now look like:
As you can see the /opt directory has been added proceeding a ":". Complete this addition and save the file. You're not done yet.
If you issue the command echo $PATH you will still not see /opt in the users' $PATH. Why? You have to log out and log back in before this change will take effect. So log out, log back in, and issue the command again. Issuing the command echo $PATH will not issue:
Any command found in the /opt directory is now global.
The $PATH is a very powerful tool to take advantage of in Linux. By using it you can install applications in directories outside of the norm and still make them global. I often install applications in the /opt directory or will create a /data directory for a more temporary application installation.Advertisement
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.