Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Adding a directory to the PATH in macOS Monterey?

I need to add a folder /opt/xyz/bin to the PATH in macOS 12.6. I've looked at some articles and discussion posts, but they appear to apply to previous macOS versions and I haven't found an approach that works yet. Any possible help? As of now, my login shell is shown as bash, but I'd be happy to change it to zsh if this would help.



MacBook Pro 15″, macOS 12.6

Posted on Sep 23, 2022 10:35 PM

Reply
Question marked as Best reply

Posted on Sep 24, 2022 4:08 AM

The question is whether that path /opt/xyz/bin has any application in it that would pose a name space collision with an application distributed with the operating system, or if you just want its contents found. Where one places that path would then determine which same named application would be used first. The PATH statement is essentially shell independent other than the file it is located in.



/Users/$USER/.bash_profile or /Users/$USER/.zshrc


export PATH=".:/opt/xyz/bin:$PATH"


or


export PATH=".:$PATH:/opt/xyz/bin"



Once you have saved the dot file, you can invoke the PATH changes as follows for the relevant shell, or upon quitting and reopening the Terminal application, the PATH changes will be current when the dot file is read:


source ~/.bash_profile
source ~/.zshrc


Similar questions

11 replies
Question marked as Best reply

Sep 24, 2022 4:08 AM in response to FrankO23

The question is whether that path /opt/xyz/bin has any application in it that would pose a name space collision with an application distributed with the operating system, or if you just want its contents found. Where one places that path would then determine which same named application would be used first. The PATH statement is essentially shell independent other than the file it is located in.



/Users/$USER/.bash_profile or /Users/$USER/.zshrc


export PATH=".:/opt/xyz/bin:$PATH"


or


export PATH=".:$PATH:/opt/xyz/bin"



Once you have saved the dot file, you can invoke the PATH changes as follows for the relevant shell, or upon quitting and reopening the Terminal application, the PATH changes will be current when the dot file is read:


source ~/.bash_profile
source ~/.zshrc


Sep 24, 2022 10:11 AM in response to FrankO23

You will use command-line editors such as vim or emacs or pico to edit the file—if you want/need to use a GUI editor, use BBEdit or textedit and select plain text—and make your changes.


pico is the easiest command line editor to use of the three, as it shows the editor commands on the screen.


BBEdit is a commercial product with a useful free tier. The free tier does all you’ll need here, and more. The free tier within BBEdit replaced the free TextWrangler editor.


To edit one of the bash login scripts:

pico ~/.bash_profile

or if you’re setting up zsh:

pico ~/.zshrc


About shell scripts in Terminal on Mac - Apple Support

Use command-line text editors in Terminal on Mac - Apple Support (this lists nano, but you’ll actually get pico. For discussion and usage purposes here, pico and nano can be considered the same.)

Command Line Primer


If you’re unsure about using the shell, or about a particular shell command, then I’d tend to suggest creating and using a second separate login for experimentation and testing. While you can’t damage macOS Monterey itself, an errant shell command can potentially damage or delete your own local files. Be very careful with -R “recursive” commands for instance, on chmod and rm and ilk. Have backups, etc.


Sep 24, 2022 8:15 AM in response to FrankO23

To expand on the above, PATH is nothing more than a list of places to look for filenames that match your commands as input.


This so you don’t have to type the path on each command you input.


First matching filename found in the list wins.


If you want, you can bypass PATH entirely and add the full path onto each command, though.


Entering the full path is one way to get out of trouble if a name collision occurs, or if some other problem with PATH arises, too.


The default shell—bash, zsh, ksh, etc—doesn’t matter here, save for the filenames of the scripts run at login or at logout.


Sep 24, 2022 9:13 AM in response to VikingOSX

Thanks much. Using:


export PATH=".:$PATH:/opt/xyz/bin"


appended /opt/xyz/bin to the end of PATH. However, I see that some of the existing paths are now repeated, and "." has been added as a path. (Not quite sure how this happened, likely an error on my part.) There are also some other issues it would be nice to clean up, such as multiple paths for different versions of Python only one of which is now used.


Is there a suggested way to edit PATH?


Sep 24, 2022 9:42 AM in response to MrHoffman

MrHoffman wrote:

Old school: I edit PATH in the shell’s login script, and then cut-and-paste and invoke the export when the edit is completed. That makes the change persistent, and tests the results before logout.


Thanks. Sorry to be new to this, but could you be more specific about what file I'd be editing, and any additional steps I'd need to take (i.e. to invoke the export)?

Sep 24, 2022 10:15 AM in response to FrankO23

Here is my PATH from my .zshrc file on macOS Monterey 12.6. I use Python.org installers and have therefore, re-added Python 2.7.18 and Python 3.10.7 into /Library/Frameworks/Python.frameworks. Both paths are needed in my PATH entry. I also use homebrew package manager and on my M1 Mac, it deposits built packages in /opt/homebrew/bin. I also have a local bin directory for custom scripts, and some executables in /usr/local/bin too, including the links from the Python distributions.


The leading dot means find things in the current directory, so I don't have to prepend scripts with ./scriptname.


export PATH=".:$HOME/bin:/Library/Frameworks/Python.framework/Versions/3.10/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/opt/homebrew/bin:${PATH}"



Sep 24, 2022 10:40 AM in response to MrHoffman

MrHoffman wrote:

To edit one of the bash login scripts:
pico ~/.bash_profile


Thanks. When I use pico to open ~/.bash_profile as above, the file consists only of PATH and export statements to add three specific paths -- two for Python versions, one for MacPorts. However, it does not include a number of other paths that I see if I execute "echo $PATH", including the /opt/xyz/bin that I just successfully added and tested to work. Are these found elsewhere?


Sep 24, 2022 11:02 AM in response to FrankO23

Your login scripts are adding paths, yes.


When you successfully added /x/y/z with an export command, it’s live. Volatile. Not persistent. What’s in the login scripts is what you’ll get at each subsequent login. This is why I edit the login scripts, then export. Or can logout and back in.


Here’s an intro to the bash logins: https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_01.html

Here are some macOS wrinkles: https://scriptingosx.com/2017/04/about-bash_profile-and-bashrc-on-macos/


Check all the spots where PATH is modified. A Spotlight search for “export PATH” or such from the GUI—or as you become more experienced with and more comfortable with the command line—a grep or mdfind or such can usually find the modifications to PATH.

Sep 24, 2022 3:09 PM in response to MrHoffman

MrHoffman wrote:

Check all the spots where PATH is modified. A Spotlight search for “export PATH” or such from the GUI—or as you become more experienced with and more comfortable with the command line—a grep or mdfind or such can usually find the modifications to PATH.


Thanks much for the comments.


I decided to change to zsh, and noticed that when I did so the contents of .bash_profile didn't come along, so I edited .zshrc to reinstate the Python paths and my new /opt/xyz/bin path. This worked fine.


I noticed that in addition to the paths that macOS places in PATH by default ( /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin ), there were a couple of paths that survived the transition to zsh and that were never in .bash_profile. For example, one for Little Snitch. This made me curious about where/how the Little Snitch path is getting inserted into PATH.


I then changed to the root directory and ran sudo grep -R 'export PATH' which produced 46 lines of output. So I instead tried a grep from the root directory looking for a line including the Little Snitch path. Apart from some "Operation not permitted" errors, nothing is turning up. Well, not the end of the world, I was just curious.



Adding a directory to the PATH in macOS Monterey?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.