ghacks Technology News

Adding a directory to your $PATH


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:

echo $PATH

You should see something like:

/usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/jlwallen/bin

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.

.bash_profile

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:

PATH=$PATH:$HOME/bin

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:

PATH=$PATH:$HOME/bin:/opt

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:

/usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/jlwallen/bin:/opt

Any command found in the /opt directory is now global.

Final thoughts

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.




Tags: , ,
Categories: Advice, Linux, Open Source, Tutorials Basic



Related posts:

Get To Know Linux: The /etc/init.d Directory
Command Line Directory Switcher
The different ways to execute a Linux application
Give users specific access with sudo
Recursively encrypt directories with gpgdir
Securely copy files with scp
Get to Know Linux: File System Hierarchy
Getting to Know Linux: Installing From Command Line

4 Responses to “Adding a directory to your $PATH”

  1. Ralph says:

    You might want to mention that this will only work if you use bash for your shell.

  2. jack says:

    @Ralph: Thank you. Forgot to mention that. I added a Note.

Trackbacks/Pingbacks

  1. [...] located within your users’ $PATH. NOTE: You can learn more about this in my article  “Adding directories to your $PATH“. To find out what directories are in your $PATH issue the command echo $PATH. What you see [...]

Leave a Reply   Follow Ghacks   Subscribe To Comment Rss

© 2005-2009 Ghacks.net. All Rights Reserved. Privacy Policy - About Us