OS X: Change your PATH environment variable

When you run a command from a UNIX or UNIX-like shell, the shell looks for the executable file using the directories listed in your PATH variable as a map. For convenience, adding directories to this environment variable means you don’t have to go hunting for a file each time you run it. Following these directions will allow you to add a directory to the search PATH.


To change your path, you must edit the .profile file in your home directory. If you are comfortable using the vi editor, you probably don’t need to read any further. If not, then you can use TextEdit to edit your .profile.

The default .profile is fairly short. The .profile is read when you open a Terminal window and each line is executed just like typing a command into the Terminal window. While this is handy because you can make any available command run whenever you drop to a shell, we’re concerned with changing an environment variable, PATH.

In your editor, find the line that starts with export PATH= and give it a look to make sure it doesn’t already contain the directory path you’re going to add. Sometimes the PATH variable can get lengthy, but chances are yours just has a few directories separated by colons, perhaps something like this:

export PATH=/opt/local/bin:/opt/local/sbin:$PATH

There are a few things to note before making changes. The format of this line is important. The use of spaces in this command, or their lack, matters. In particular, there cannot be spaces around the equals sign or between any of the directories. If there are spaces in the directory name you want to add, you’ll need to escape the space by preceeding it with a \backslash or by putting the path in quotes (export PATH=”/path/here”).

The export= keyword in front of that line has a very specific purpose. Defining a variable without exporting it makes it available only to the current shell, not to any subsequent shells. You may think to yourself that you don’t plan to make any subsequent shells, but this happens whenever you run a shell script. If the PATH variable was not exported, when you run a shell script, the PATH would no longer exist and it is possible that the script would fail.

The $PATH at the end of the example above tacks the previous value of the variable (if it exists) onto the end of the PATH. By default, this will add ‘:/bin:/sbin:/usr/bin:/usr/sbin’ to the end of the PATH for you which is useful because most everything you run from the command line lives there.

So, to add a new directory to the path, simply add it to the existing PATH line in .profile being careful to separate it from other directories there with colons and careful not to introduce unwanted spaces (everything after the space will be ignored). For example, to add the directory /mightyq/bin to the PATH shown above, the line could become any of the following examples:

export PATH=/mightyq/bin:/opt/local/bin:/opt/local/sbin:$PATH
export PATH=/opt/local/bin:/mightyq/bin:/opt/local/sbin:$PATH
export PATH=/opt/local/bin:/opt/local/sbin:$PATH:/mightyq/bin

Note that in the third example the new directory is added to the end of the PATH. You have the ability to optimize the searches your shell will do on your behalf each time you run a command by organizing your PATH logically. Putting less frequently used or really massive directories later in the path may give you a little performance boost (although these days things are pretty fast, so you have to be a little anal to really enjoy this).

If you don’t need a directory in your path, you can reverse the process by deleting the unwanted directory still taking care to preserve the no spaces, colon separation rules.

One last note, to test the change you made, you can use the echo command, but you need to make the shell reload the .profile first. Assuming you are in your home directory (if not, running ‘cd’ without any options will take you there), run these commands:

. ./.profile
echo $PATH

The first is a neat little command in that it shows three uses or interpretations of the period in a single line. The first . is a shortcut to cause the shell to ‘source’ or load the contents of the subsequent file as itself, in the manner that the shell uses when you login to a system or start a Terminal window. If you simply executed these commands like a shell script (bash .profile, for example) you would start a new shell, that shell would get the variable set, and at the end of running the .profile script, that new shell would cease to exist and the newly defined variables would be relegated to the missing sock universe.

The second period means the current working directory. It’s not compulsory in any way in this command, but it’s habit from explaining the dots to folks, so I type it all the time now. In this context, you could also use ~/.profile as ~ explicitly means your home directory.

The last dot causes the .profile to be hidden from view in a normal directory listing or Finder view. It doesn’t change the file in any other way, it just make it invisible and de-clutters your directories. To see hidden files, you can use ‘ls -a’ and you might be surprised by what you find.

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

35 Responses to “OS X: Change your PATH environment variable”

  1. March 08, 2009 at 8:44 am, david said:

    I have a question: When I “echo $PATH” or type “env” which shows the same path I assume, I’m given a path that has a bunch of variables, namely /usr/bin/:/usr/bin/sbin, etc etc that I can’t seem to find in my path files. Not in ~/.bashrc, ~/.profile, or anywhere else I’ve looked…where do those variables reside. I really just want to switch the order of /usr/bin and /usr/local/bin so my /usr/local/bin python2.6 install default starts instead of the version 2.5 that ships with Mac.

    Hope that’s not too confusing, I don’t think it is.

    Thanks for the otherwise helpful information…after adding a ~/.profile I noticed that the PATH in there precedes the other PATH attributes mentioned above so temporarily I’ve tacked /usr/local/bin on there and it works how I want…but now I have /usr/local/bin on the PATH twice and would like to remedy that.

    Cheers

    Reply

    • April 04, 2009 at 10:24 pm, tombeek said:

      david: it’s ok to have the same elements of a path variable in your path more than once; in some cases it is necessary. As far as how to give one path priority over another, just make the one you want to load come first (reading left to right) in your PATH variable.

      Reply

      • May 13, 2010 at 2:13 pm, Chris said:

        i’d love to see a reply to Davids Question :)
        He asked …
        “I can’t seem to find in my path files. Not in ~/.bashrc, ~/.profile, or anywhere else I’ve looked…where do those variables reside.”

        i.e when you make a change in sudo bash login … WHAT exactly are you changing … deff not any of those files in root user dir for sure …

        Any one know the answer … i don’t think so because everyone avoids this question …

        Reply

        • July 05, 2010 at 9:22 pm, Frederik said:

          I found the path files at /etc/paths Hope this works for you.

          Reply

  2. April 12, 2009 at 11:21 am, marcel said:

    normal i would add an exra PATH in /etc/profile

    Reply

  3. July 05, 2009 at 6:53 am, Mitch said:

    Thanks mate,
    Just what I was looking for. It seemed to be a different way of doing things in Leopard than previous versions.
    Cheers,
    Mitch

    Reply

  4. October 04, 2009 at 1:54 pm, Amgine said:

    In my case, I need to add a directory to the path for both myself and for www-data. Is there a variation for adding the dir in /etc/profile?

    Reply

  5. November 13, 2009 at 9:55 pm, Cavin Graves said:

    For some reason this is what my path is:

    [code]
    ##
    # DELUXE-USR-LOCAL-BIN-INSERT
    # (do not remove this comment)
    ##
    echo $PATH | grep -q -s "/usr/local/bin"
    if [PATH=$PATH:/usr/local/bin
    export PATH
    fi
    export CLICOLOR=1
    export LSCOLORS=ExFxCxDxBxegedabagacad
    # Setting PATH for Python 3.1
    # The orginal version is saved in .profile.pysave
    PATH="/Library/Frameworks/Python.framework/Versions/3.1/bin:${PATH}"
    export PATH
    [/code]

    This is even though I have Fink installed. I'm sorry to post this so randomly here but I don't know where else to put it.
    When I use Fink, it works.... So I don't really know why my profile has this in it. this is in ~/.profile just so you're sure.

    If you want to see what echo $PATH does this is it:

    [code]
    /Library/Frameworks/Python.framework/Versions/3.1/bin:/sw/bin:/sw/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/opt/local/bin:/usr/X11R6/bin
    [/code]
    So it's not as if I have a problem as of right now. But for some reason, as far as I can see I don't know where to edit my path. I'm sure I'll want to eventually.
    Thanks.

    Reply

  6. January 03, 2010 at 12:57 am, Jason Woan said:

    Many thanks! I’ve been wondering how to do this for some time now.

    Reply

  7. January 05, 2010 at 12:07 am, High Five said:

    When I start a new terminal, and echo $PATH it does not contain the change. But when I type vi ~/.profile I see it.

    What could be going on?

    Reply

    • January 22, 2010 at 1:21 am, Guest said:

      no

      Reply

    • January 19, 2011 at 4:23 am, Rara said:

      close terminal and reopen and the changes should be saved.

      Reply

  8. February 04, 2010 at 4:37 pm, Clark said:

    Doesn’t work on OS X 10.5.8

    Reply

  9. March 30, 2010 at 10:42 pm, Anonymous said:

    I used this to get quick access to mysql. instead of typing /usr/local/mysql/bin/mysql … if you ADD
    PATH=$PATH:”/usr/local/mysql/bin/”
    export PATH
    to the gumph already there… you only have to type ‘mysql’ into the terminal to run it – because it adds PATH to the other list of domains that it searches when it asks itself “what could he possibly mean by ‘mysql’ ?” Cheers. I added this because i wasn’t clear that you could state PATH more than once without overwriting it (which you can – it seems…)

    Reply

  10. June 20, 2010 at 9:45 pm, James said:

    when I try to echo $path i get a blank line in Terminal. I want to setup Ruby on Rails in my usr/local/bin folder but can’t seem to get confirmation that I have changed the PATH variable.

    Reply

    • July 05, 2010 at 9:20 pm, Frederik said:

      I found that you have to use capital letters, try echo $PATH

      Reply

  11. July 15, 2010 at 5:09 pm, Mitur Binesderti said:

    Sorry but this is completely wrong and there is no .profile file. This must be REALLY old or something.

    To ACTUALLY change your path type: sudo pico /etc/paths

    You can then edit your path variable and it will be remembered and usable by all users.

    Reply

    • August 03, 2010 at 2:32 pm, fx-mike said:

      If you do not have a .profile file, then just create one (in your home directory of course)

      Reply

    • October 05, 2010 at 10:09 pm, Tired Of Knowitalls said:

      This is not wrong.

      The same path variable is not always desired for all users. And… not everyone uses pico.

      Only use /etc/paths:
      1) If you are SURE of what you are doing
      2) You read what you edited/typed at least twice
      3) and if ALL users on that machine need the same stuff in their path variable.

      So there.

      Reply

  12. February 08, 2011 at 7:45 pm, Andreas Hagemann said:

    How do I create a .profile file.

    //PC-user for 20 years…

    Reply

    • July 26, 2012 at 6:05 pm, MG Lim said:

      create a .profile
      Open terminal and type
      > touch ~/.profile
      > open -t ~/.profile

      Reply

  13. February 11, 2011 at 12:46 am, Sdlfkjsdlfksjdf said:

    I did all that’s posted here and PATH is unchanged.
    Mac must be more fail than I initially thought.

    Reply

  14. August 22, 2011 at 6:21 pm, mike said:

    very well explained, thank you

    Reply

  15. December 29, 2011 at 1:10 pm, split said:

    works great!
    thank you very much!

    Reply

  16. December 31, 2011 at 5:03 am, Sachin khosla said:

    You may simply do – sudo vi /etc/paths

    :)

    Reply

  17. March 05, 2012 at 7:42 am, Software Maniac said:

    Question regarding Lion (Mac OS X 10.7.x)… Funny, I can’t seem to find the .profile file in my home folder. Even with hidden files visible. Is there any other place to find this?

    Thank you.
    Ken

    Reply

  18. March 15, 2012 at 5:23 pm, malena said:

    @Ken-
    I’ve been searching for the same thing. There is a profile document (along with my bashrc and cshrc like I’m used to in Linux) in /etc. Although it’s root owned, and not automatically editable by me. Anyone else know how to edit these? Or where the ones I should be editing are located?

    Also, I’ve read elsewhere online that gcc is standard, but when I try to run it, I get a “command not found.” Simply installing xCode didn’t change anything. Do I need to add something to my path (and if so, how?)?

    Reply

  19. June 15, 2012 at 8:12 pm, Ted Toal said:

    I can set environment variables in .profile and they show up as such in command shells, but they do not show up in other programs such as my R programming environment. It does not see environment variables defined in .profile, even after a reboot. I suspect that a log-in does not run .profile. There are other files that also can have environment variable definitions, and depending on where you want them to appear (or how early in the log-in sequence), you may need to define them elsewhere. ???

    Reply

  20. August 25, 2012 at 5:42 pm, Jeffrey Walton said:

    > “I did all that’s posted here and PATH is unchanged….”
    try `source ~/.profile`

    Reply

  21. September 01, 2012 at 11:17 pm, Denis Kabistan said:

    You can also type the following command to refresh your terminal without quitting.

    Type this > exec $SHELL

    Reply

  22. October 21, 2012 at 11:27 am, Peter said:

    Why do you write ANDROID – APPLE – WINDOWS instead of Google – Apple – Microsoft? Apple is the name of a company, not an OS.

    Reply

  23. February 19, 2013 at 5:22 am, Giulio said:

    Hello Quinn, you’re writing about deleting an item from the path variable, but you don’t explain how. How do aI delete an item off the path variabel via command line?
    Regards,
    Giulio

    Reply

    • February 19, 2013 at 4:53 pm, Quinn McHenry said:

      Hi Giulio! It’s actually pretty simple to delete something from a variable like this, but just not obvious. Variables are fleeting things. They don’t live after the shell goes away, so they get setup each time a shell opens (like when starting Terminal). They also aren’t set like a setting in the operating system.. they are temporary. So, if you want to delete something from the PATH variable, all you have to do is overwrite it (set it again) without the value you want to delete. So, if you don’t want /opt/local/bin in your path, just leave it out. Make sense?
      Quinn

      Reply

  24. January 29, 2014 at 3:16 pm, Daniel Ansong said:

    Great article, this had greatly clarified /.bash_profile and $PATH environment variable for me. THANKS!

    Reply

  25. February 01, 2014 at 5:44 pm, Isaiah kang said:

    hey I am from maplesoft.com and how do i set up the PATH for maple 17?
    P.S. I have no clue what PATH is

    Reply

Leave a Reply