How to Add a New Path to PATH at Command Line the Right Way

Aug 14, 2014 - 6 Comments

Terminal The user path is the series of directories that command line programs are searched in order to run. For example, if you type ‘iostat’ into the terminal, then iostat will be run from /usr/sbin because “/usr/sbin” is part of your $PATH. Accordingly, users who frequently use the command line may need to adjust or add new paths to their terminal so that commands can execute properly.


Perhaps obvious by now, modifying the PATH is aimed at more advanced users and developers who are using the Terminal and spending a lot of time at the command line. Average Mac users typically have no need to change, add, or otherwise adjust this in Mac OS X. Speaking of Mac OS X, while this is obviously Mac centric, you can use this same trick to add a PATH to your shell in Linux too, as well as most other unix flavors.

Before getting started, you might want to see the existing $PATH just in case you mess something up, that way you can easily restore to it by exporting using the same commands. To check the current $PATH simply type: “echo $PATH”

Adding a Directory to PATH

The easiest way to add a new path to $PATH (the environment variable) is with the export command. In this example we’ll add “~/opt/bin” to the user PATH with export:

export PATH=$PATH:~/opt/bin

You can run that directly from the command line, then check the $PATH with echo to show that it has been added like so:

echo $PATH

This should return something like the following, note the newly added ~/opt/bin directory at the end:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/osxdaily/opt/bin

Add a new PATH to $PATH in Mac OS X Terminal

How to Add Multiple Paths to PATH

Just as multiple paths can be stored and strung together in $PATH in accordance to their search priority, you can also add new paths this way as well. We’ll use the same example as before, but this time also add the ~/dev/bin directory:

export PATH=$PATH:~/opt/bin:~/dev/bin

Setting PATH in Shell Profile

Keep in mind that for changes to PATH to persist, you’ll want to add them into ~/.profile, .zshrc, or ~/.bash_profile, depending on which shell you use. Use your favorite text editor to do that, whether it’s nano, emacs, or vim. If you’re getting complicated, it’s a good idea to add comments to the .profile to keep things easily scannable:

#Adding opt bin and dev bin to PATH for fun
export PATH=$PATH:~/opt/bin:~/dev/bin

Bash is the default shell in many versions of Mac OS X, but zsh is the default in later modern releases, and of course there are other shells like sh, ksh, and tcsh are also bundled with the Mac. Changing the Mac OS X shell is a very easy process either with chsh, or within Terminal and/or iTerm2 preferences.

.

Related articles:

Posted by: Paul Horowitz in Command Line, Tips & Tricks

6 Comments

» Comments RSS Feed

  1. Damien Brock says:

    Try to use Long Path Tool, it really can help you with that.

  2. Luc Bizier says:

    How do you make changes to PATH to persist?
    You’ll want to add them into ~/.profile
    But how to modify the ~/.profile?

  3. Sylfraor says:

    One way to permanently add path on the whole system in a clean way is to use the /etc/paths.d/ directory.

    inside /etc/paths.d/, one could add files, each of these files contains only the path, without any export command.

    for example, you could do
    sudo nano /etc/paths.d/x11
    and write only the line
    /opt/X11/bin

    • Nevin Williams says:

      Great advice, Sylfraor.

      This sidesteps the whole Bourne Shell/C shell syntax awkwardness regarding ‘export’ and ‘setenv’.

      After a new file is added to the /etc/paths.d/ directory, a new shell has to be opened to see the effect.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site