Choosing the Right $PATH

Sorry for the long delay, but I have been busy with getting ready for school again and moving into my new apartment.

This is in some ways a continuation of my post Changing Modes.  As pointed out in one of the comments (thanks to Miorel) some people might wonder why you would have to type ./hello_world instead of just hello_world in order to execute the Perl script.  Well I though about this for a second and thought, “Well to run a script that isn’t one implemented by the OS you must place ./ before it.  This tells the OS that you are trying to execute a file.”  For example, if you want to run Firefox you just need to type firefox and not ./firefox.  If you are curious-and you should be-and enter ./firefox into the terminal you should get the following message, “bash: ./firefox: No such file or directory” (unless you for some reason have a file or directory called ./firefox, but why would you?).

Though that might answer might be good enough for some, I wanted to know why this was so, and if it was possible to change it so that you would only need to type hello_world (and not ./hello_world).  This is where $PATH comes into play.  I am going to try my best to explain this, and will also provide links to other sites for further reference.

To begin with here is a one line definition-which sadly isn’t mine, but gets the point across-of what the PATH variable is.

PATH is a list of directories where commands are looked for.

So when I type echo $PATH into the terminal I get the following:


Returning to our original example; if you were to type hello_world into the terminal. The OS would first look for a command (a file with the x bit set) with that name in /usr/local/sbin. If one is found it is run, if not it continues onto /usr/local/bin, and so on.

Now if we would like to only have to type hello_world, all we would have to do is add its directory to the PATH variable. Suppose that our script is in the directory /Mine. First I will show you the WRONG WAY to do this. Then two different correct ways (though I am not all that sure about which is better to use as of yet).

This is wrong because it will replace the original PATH variable with /Mine, and I am sure that you don’t want this.

Correct way #1: PATH=/Mine:$PATH
Correct way #2: PATH=$PATH:/Mine

As you can see, the first one will add /Mine to the beginning, and the seoncdd to the end of the PATH.

Here are two sites you can visit if you wish to learn more about the PATH variable. and


One Response to Choosing the Right $PATH

  1. Miorel says:

    Thanks for the shoutout 😉

    Incidentally, if you ever execute ls -a you may notice the special files “.” and “..” which represent the current and the parent directory, respectively.

    So, some people who get tired of prefixing local scripts with “./” will add “.” to the $PATH. But this is a bad idea for a couple of reasons. Firstly, you might accidentally give a script the same name as an existing command, and the shell will run whichever it finds first when looking at $PATH. This may or may not be the one you want. Secondly, it’s a potential security hole. Suppose “.” is part of your path. Then someone can prepare a script named ls for you, so when you go in its directory to try and get a listing, instead of /bin/ls you’ll be doing something completely different…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: