Setting default PATH variables on Mac OS X Leopard

I've been using Unix, Linux and the variants for a very long time.  My first version of installed Linux was 0.91-something.  I even had Coherent installed at one point in the late 80's.  I've worked and played with most variants of *nix, including *Aix, Xenix, SCO, HPUX, Redhat, Ubuntu, CentOS, and several others that I just can't think of off the top of my head. Point being that my familiarity made me an early-adopter of the Mac OS when it was announced that they would begin building on BSD Unix.  Up until that time, I was pretty much a "Mac is for Graphics-Dudez" kind of programmer.  Besides, this being the late 80's through the late 1990's, I really couldn't afford one.

See, the thing about Unix-based systems, for you non-Geeks, is that (a) they work, (b) they work really well, and (c) except for the occasional Redhat distribution, you can pretty much find all the files you're used to finding in the places that you're used to looking for.  In other words, if I want to change my terminal login message, I know to edit the file: /etc/issue and I can change the login message to something really snarky.

But, as I said earlier, Max OS X is based on that there left-handed Unix coming out of Berkeley ... so not everything is in the places that you'd expect.  In this quick article, I'm going to share what I learned today about setting up your PATH environment variable for your default shell.

Ok - for you newbs,  your PATH variable defines the directories that that the shell interpreter will search for programs when you type a command-into the shell interpreter using the command line interface.  Yes, that's right - no mouse.  We're using the terminal here.  Very old school, according to my 16-year old.

In a Mac terminal, you can see what your PATH value is by typing in the following command:


[cc lang='bash' ]

Mike-Shallops-MacBook-Pro:conf mikeshallop$ echo $PATH /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/share/php/bin:/usr/X11/bin


The shell interpreter searches each one of these locations, separated by the ":" character, starting with the first one, and moving through each one in the chain until it either runs out of places to search, or it finds the command that you typed.  For example:

[cc lang='bash' ]

Mike-Shallops-MacBook-Pro:conf mikeshallop$ foobar -bash: foobar: command not found


I typed the command foobar a the bash prompt and was told, in return, that the command was not found.   The bash shell searched each one of the directories in my PATH looking for foobar and, had it found the little dickens, it would have executed (ran) it.

Which brings us to how to add directories to your default PATH variable.  Fortunately, there are several ways to do this.  We'll start with the least-optimal and end with the recommended.

Option 1 --- Adding the location in real-time:

The first option allows you to dynamically add a location to the PATH variable from the shell interpreter (command line) itself.

[cc lang='bash' ]

Mike-Shallops-MacBook-Pro:conf mikeshallop$ echo $PATH /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/share/php/bin:/usr/X11/bin Mike-Shallops-MacBook-Pro:conf mikeshallop$ PATH=/home/foobar/bin:$PATH Mike-Shallops-MacBook-Pro:conf mikeshallop$ export PATH Mike-Shallops-MacBook-Pro:conf mikeshallop$ echo $PATH /home/foobar/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/share/php/bin:/usr/X11/bin


In the session above, I echo'd my current PATH to show you the contents.  The second command, I added the directory /home/foobar/bin by prepending it to the current PATH, using the ":" separator, and making sure I proceeded the PATH variable name with the "$" so that it would evaluated.  Otherwise, I would have ended up with: "/home/foobar/bin/:PATH".

Next, I export the PATH -- what this means is that you're making the changes to your path available to any sub-shells that you fork off the current one.  If that sentence made your eyes glaze like last Christmas' ham, don't worry about it.

Finally, I re-echo the PATH variable so that you can see that shell will now, indeed, search the new directory first, before it searches any other directory.

The downside to the this method is that it exists only for your current session.  The moment you exit your terminal session, (logout), the change(s) you've made to your PATH variable have been lost.  Forever.  Buh-bye.  Which brings us to option 2:

Option 2: Changing Your Path for All Sessions

In your home directory, there's a file called: .profile.  Within .profile, you're allowed to enter any valid shell command (like adding to your path, creating an alias, or writing a function) that you'd like.  You can add to your PATH in this file and, forever after when you login and this .profile file is available to you, your new PATH will exist for your SHELL session.

That's pretty much all there is to it.  You can edit the file using any text-editor (you are using vi, right?) and make the changes in the same way that you did at the command line, except all you really need is the PATH= and the export PATH commands.

Option 3 - Changing everyone's path

The third and final option is to change everyone's path so that when everyone logging into your system gets a terminal session, their paths are all set to the same changes that you've established.

Now, remember when we were talking about how *nix systems are homogeneous -- that the files and configurations are pretty much the same regardless of which flavor you've happen to have chosen?  Well, turns out, that's not quite soooo true with the Mac.

Normally, the PATH variable is set in a file called: /etc/profile.  For everyone.  However, on Snow Leopard, what you see when you find this file that that up to two things will happen:

  1. The shell tests to see if an executable (program) exists called path_helper -- if it exists, then it is executed (ran)
    1. If path_helper is ran, the program will search the directory /etc/paths.d and any files it finds, the contents will be examined and added to the current PATH.
    2. So, for example, once you've installed PEAR on your system, you need to add the directory where the PEAR executables live.  So you would do the following:
      1. Create a new file in the /etc/paths.d directory:  touch PEAR
      2. Edit the PEAR file and add your path(s) (one per line) to the file:
        1. /usr/share/php/pear/bin .... for example...
  2. The second thing done by /etc/profile is to test to see if you're running the BASH shell.  (There are many different kinds of shells, but this one is mine.)  If you are running the BASH, and chances are, you are, then the shell commands found in /etc/bashrc are executed and added to your environment.  It is within this directory that you could add the new PATH location.


So, summary time -- once you've used one of these three methods, you'll have access for however long, or short as the case may be, of a time that you'll need them.  Often I find it useful to add directories for scripts and other downloads that I want to have installed outside of my root filesystem (space considerations) so I place them outside of their normal repositories, remembering to update the PATH variable so that I, or the other users on my system, can access the new commands.

Hope this helps!