Adding a directory to your $PATH - gHacks Tech 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.

Advertisement

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.

If you like our content, and would like to help, please consider making a contribution:


Previous Post: «
Next Post: »

Comments

  1. Ralph said on June 3, 2009 at 11:25 pm
    Reply

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

  2. jack said on June 4, 2009 at 1:51 pm
    Reply

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

  3. Panorain said on March 25, 2014 at 10:36 pm
    Reply

    Thank you for this article. I cannot seem to find .bash_profile anywhere on my computer though. Ubuntu 10.04 is currently installed.

    paul@paul-desktop:~$ sudo updatedb
    paul@paul-desktop:~$ locate -w .bash_profile
    /usr/share/doc/adduser/examples/adduser.local.conf.examples/skel/dot.bash_profile

    That’s all that shows up.

    Any Ideas?

    Thank you,

  4. chuck said on December 31, 2014 at 5:23 am
    Reply

    You do not have to log out and log back in to get this to work.
    From the command line:

    cd
    souirce .bash_profile

    the ‘cd’ is to get you to your home directory where .bash_profile resides.

    Enjoy.

    1. Panorain said on December 31, 2014 at 2:31 pm
      Reply

      With the newer Mint 17 now I am unable to find .bash_profile file in /home directory.

      paul@desk/paul-Lenovo ~ $ locate -w .bash_profile
      /usr/share/doc/adduser/examples/adduser.local.conf.examples/skel/dot.bash_profile

      i’ve got a:

      -rw——- 1 paul paul 8.8K Dec 31 07:22 .bash_history
      -rw-r–r– 1 paul paul 220 Dec 11 23:18 .bash_logout

      I just did a fresh updatedb ………..prior

      What kernel are we talking about I could look into the 2.6 kernel again but as always you know /etc/environment is/was a ‘Deep SystemWide Approach’ to adding /opt .

      Write me back when you have time.

      Thank you,

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.