Adding to the PATH variable on Mac OS X

Apr 14

Adding to the PATH variable on Mac OS X can be done through lots of ways. Before getting into these ways you should know that PATH is nothing but an environment variable that holds set of directories where executable programs are located.

First, Lets start by retrieving the current PATH contents. To print the current settings, open the Terminal and type:

The output of the above command will be something like:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

As you see, it’s a list of directory names separated by colon : character.

Please note that some of the following files does not exist by default, so create them whenever needed.

As said before adding to the PATH variable on Mac OS X can be done through lot of ways. The main difference between each of these ways is the scope. Here are a list of some possible methods:

  1. .bash_profile
  2. When you start the Terminal app in OS X you get a bash shell by default. When the bash loads it runs the .bash_profile script. Adding a new path to this file will make it available to the current user while using the bash shell only. This files is located in

    ~/.bash_profile

    In order to add into this file you need to add the directories with format KEY=value with no spaces around the equal sign. Also you need to add the export key word in front of the variable or after it. Below is an example of variables:

    Or:

    Tip: You need to rerun the terminal in order to reload these new variables or you can run the following:

  3. .profile
  4. This is the original profile configuration for the Bourne shell (sh). Adding a new path to this file will make it available to the current user while using both of the Bourne and bash shells. This file is located in

    ~/.profile

    Adding variables to this file follows the same syntax as adding variables to .bash_profile.

    Tip: You need to rerun the terminal in order to reload these new variables.

  5. /etc/paths and /etc/paths.d
  6. These are used when setting a system wide variable as it gets set early in the process of shell creation. This mean that the variables set to those locations will be available to all users. The main difference here is /etc/paths is a file that directly holds the paths while /etc/paths.d is a directory known as path_helper which is the recommended way by Apple.

    The default content of /etc/paths are:

    /bin
    /usr/sbin
    /sbin
    /usr/local/bin

    You can add any new path to a new line there and it will be added for you. Alternatively you can use the path_helper to do the job for you.

    The path_helper utility reads the contents of the files in the directories /etc/paths.d and appends it’s contents to the PATH environment variable. Files in this directory should contain one path element per line.

    If you want to add additional value to your PATH, you just have to create a new file in /etc/paths.d and put, one per line, the new ones. You can also achieve that by running the following command from the terminal:

    Tip: You need to log out and log in again in order to reload these new variables.

  7. /etc/launchd.conf
  8. Environment variables set in /etc/launchd.conf will be available to all applications on the system (including the Terminal). Here is how the content should look:

    setenv GROOVY_HOME /usr/share/java/groovy
    setenv GRAILS_HOME /usr/share/java/grails

    Tip: You need to reboot your mac in order to reload those new variables or run the following (don’t forget to relaunch the process that should use the new variable):

Finally, The above are not a list of all the possible ways to add to your PATH, but I believe it contains the most handy ones.

See also:

A good explanation of login vs non-login shells
Set an Environment Variable in Mac OS X – /etc/launchd.conf
Mastering the path_helper utility of Mac OS X

Leave a Reply