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

Aug 14, 2014 - 2 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 OS X. Speaking of 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.

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 OS X Terminal

Adding 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 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 OS X, but zsh, sh, ksh, and tcsh are also bundled with the Mac, and changing the OS X shell is a very easy process either with chsh or within Terminal and/or iTerm2 preferences.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

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

2 Comments

» Comments RSS Feed

  1. 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 for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

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