Choosing the Right $PATH

August 23, 2009

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


July 10, 2009

Well today I decided to learn Bash.  One of the tutorials (though not all that great) had a pretty good introduction on some terminal commands.  The best job was done with the echo command.  First I’ll show some examples then explain what they each do.

  • echo "hello     world"
  • echo hello     world
  • echo “Number2 Atoms Zzz…” | tr ” ” “\n” | sort

The first line will print “hello      world” to the screen, while the second will print “hello world”. Notice how the extra space disappeared.  This is because the string is not in quotation marks.  The final example will display




This is due the fact that the tr command will replace all the spaces with new lines, then sort will sort them in lexicographic order. I’ll hopefully explain more about tr in later post since it seems that it can use regx. But basically echo prints whatever follows it to the screen. Finally the pipeline, ‘ | ‘, will cause the result of running the previous command to be used as the input for the following command.  This to will be talked about later, mainly because I am tired and am going to end this post now.