OS X equivalent to adding PATH variables in Windows

Discussion in 'Mac Basics and Help' started by firstyearprof, Jun 18, 2007.

  1. firstyearprof macrumors member

    Apr 16, 2007
    Hi all,

    I'm a mac newbie, windoze vet. I'm about to install a little program on my Mac to use through Terminal. I'd like for my Mac to recognize the program without my having to type in the entire path of where it is everytime I want to use it.

    In windows (XP) days, I'd do this by placing the path to the program in System Properties -> Environmental Variables -> PATH. That way, in DOS, I could just type the program name and my machine would know where it was. Is there an equivalent for Macs? Or does OSX somehow magically know where programs are? Thanks in advance...
  2. wrldwzrd89 macrumors G5


    Jun 6, 2003
    Solon, OH
    Mac OS X does, in fact, have an equivalent. It's the PATH environment variable. Adding stuff to it isn't anywhere near as simple as in Windows, though.

    Here's a tiny little shell script that might help:
    `export PATH=$PATH:$1`>>~/.profile
    I haven't tested this little thing, but what it's supposed to do is this:
    1. Read the contents of the PATH variable
    2. Append the first argument to the end of it
    3. Append the resulting command to the ~/.profile file
    Close and reopen the Terminal for the change to take effect. Note, this script requires BASH as the shell in order to work (BASH is the default shell in Mac OS X 10.4).
  3. localoid macrumors 68020


    Feb 20, 2007
    America's Third World
    If bash is your shell you can add the path to ~/.bash_profile.

    For example, to add the path /usr/local/bin

    In terminal create/edit ~/.bash_profile
    Add the following line: PATH=$PATH:/usr/local/bin; export PATH
    Save the file
    Type: source ~/.bash_profile (to get the shell to read the file without closing/restarting terminal)

    Note: ~/.bash_profile sets for current user (only)
  4. mkrishnan Moderator emeritus


    Jan 9, 2004
    Grand Rapids, MI, USA
    Isn't the technically better way to do this to put the executable in the /usr/local directory? I.E. something like this (say abc was the executable):

    sudo mv abc /usr/local/abc
    sudo chown root:admin /usr/local/abc

    The executable will then be available to all users without needing to bother with paths.
  5. wrldwzrd89 macrumors G5


    Jun 6, 2003
    Solon, OH
    The problem with this is that most command-line programs install themselves by default in /usr/local/bin, which is NOT in the path... Is /usr/local in the default path?
  6. localoid macrumors 68020


    Feb 20, 2007
    America's Third World
    Much/most of the open source stuff tends to put (via make install, etc.) the executable in /usr/local/bin and man stuff in /usr/local/man -- much/most of system wide files ends up in /usr/bin...

    You can create symlinks, or move files, add $path, etc., but everyone seems to have their way of handling it, which is usually what method works best for that person, etc.
  7. -Alan- macrumors member

    Mar 10, 2007
    I'm a Mac newbie too and can see how all of these answers can be confusing. For what I understand, when you use the terminal, the terminal runs within a shell program. OSX's default shell is bash. You can check this by the prompt command:

    echo $SHELL

    Bash has a profile, .bash_profile, within the root of each user account. In this profile you can add items to your PATH. It is a hidden file so therefore its name is preceded with a "dot." From the prompt, you can list all files--including the hidden files--with this command:

    ls -a

    If the .bash_profile doesn't exist then you must create one.

    In the file, add this line:

    export PATH=/path/to/location:$PATH

    Edit the /path/to/location then save the file. Restart the terminal and you should be set.

    You can check the path with this command:

    echo $PATH
  8. dukebound85 macrumors P6


    Jul 17, 2005
    5045 feet above sea level
    I am trying to install NCO with these instructions. How do I add this path? The above responses weren't making too much sense as I am able to run commands from the ~/usr/local/bin. I do not have a .bash_profile

Share This Page