PATH environmental valuable in Mac Sonoma 14.3

I would like to add a path to existing PATH. I would like to know the location and file name which defines the PATH. I am using MacBook Air with Sonoma 14.3.

Windows, Windows 10

Posted on May 12, 2024 11:52 PM

Reply
Question marked as Top-ranking reply

Posted on May 13, 2024 6:17 AM

What PATH would you like to add, and does that require that path be searched before the default PATH locations or afterward? Is there a command at that added path location that would conflict with an existing system application name?


The PATH syntax is identical whether you are using a Bash or ZSH shell in the Terminal. The ordinarily hidden startup files, also known as "dot" files are preceded by that "." character which in UNIX systems, hides the files both in the Terminal and in Finder Window. In the following, ${HOME} is your /Users/username folder.


In Bash, your PATH can be set in the following files:

  1. ${HOME}/.bashrc ²
  2. ${HOME}/.bash_login ¹
  3. ${HOME}/.profile


In the ZSH shell:

  1. ${HOME}/.zshrc ¹
  2. ${HOME|/.zlogin
  3. ${HOME}/.zprofile


¹ The file that I use with each SHELL.

² Sourced in .bash_login for Bash shells.


Format:


export PATH=".:path:path:path:${PATH}:path



That leading "." means the current shell location, so I don't have to preface local scriptnames with "./" to run them. Paths are separated by colons and that example of a trailing path after ${PATH} is optional in case there is a command that you want found, but not before the other paths. I use the Zsh shell and on an Apple Silicon Mac, the homebrew package manager places installed executables in /opt/homebrew/bin — some with names that would conflict with applications earlier in the PATH, so I want them found last.



7 replies
Question marked as Top-ranking reply

May 13, 2024 6:17 AM in response to ht0422

What PATH would you like to add, and does that require that path be searched before the default PATH locations or afterward? Is there a command at that added path location that would conflict with an existing system application name?


The PATH syntax is identical whether you are using a Bash or ZSH shell in the Terminal. The ordinarily hidden startup files, also known as "dot" files are preceded by that "." character which in UNIX systems, hides the files both in the Terminal and in Finder Window. In the following, ${HOME} is your /Users/username folder.


In Bash, your PATH can be set in the following files:

  1. ${HOME}/.bashrc ²
  2. ${HOME}/.bash_login ¹
  3. ${HOME}/.profile


In the ZSH shell:

  1. ${HOME}/.zshrc ¹
  2. ${HOME|/.zlogin
  3. ${HOME}/.zprofile


¹ The file that I use with each SHELL.

² Sourced in .bash_login for Bash shells.


Format:


export PATH=".:path:path:path:${PATH}:path



That leading "." means the current shell location, so I don't have to preface local scriptnames with "./" to run them. Paths are separated by colons and that example of a trailing path after ${PATH} is optional in case there is a command that you want found, but not before the other paths. I use the Zsh shell and on an Apple Silicon Mac, the homebrew package manager places installed executables in /opt/homebrew/bin — some with names that would conflict with applications earlier in the PATH, so I want them found last.



May 15, 2024 12:59 AM in response to VikingOSX

Thank you for your response.


I would like to add PATH-a to PATH-b below. PATH-a could be searched after the default PATH locations. I believe there is no command which would conflict with an existing system application name. The PATH-a is for python3, Jupyter lab etc.


I could add it using "export PATH=$PATH:/Users/my_user/Library/Python/3.9/bin" on terminal. I would like to add this export command in a file which could be the file defines PATH-b.


PATH-a:

/Users/my_user/Library/Python/3.9/bin


PATH-b:

/opt/homebrew/bin /opt/homebrew/sbin /usr/local/bin /System/Cryptexes/App/usr/bin /usr/bin /bin /usr/sbin /sbin /var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin /var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin /var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin /usr/local/share/dotnet ~/.dotnet/tools


---


I found ${HOME}/.zprofile with the contents below and added export command to add PATH-a. It worked but I would like to know better way to add PATH-a.


# Set PATH, MANPATH, etc., for Homebrew.

eval "$(/opt/homebrew/bin/brew shellenv)"


export PATH=$PATH:/Users/my_user/Library/Python/3.9/bin --- added command by myself.


Thank you very much for your support in advance.

May 15, 2024 5:49 AM in response to ht0422

Your PATH content is excessive. Take a look at /etc/paths to see what your default paths are in $PATH. You are being needlessly redundant. I have no entry for any Cryptexes locations. I ensure that /opt/homebrew/bin is last in my PATH as I do not want Python3 dependencies in that location run before the Python3 3.12.3 that I have installed in /usr/local/bin. I use no other homebrew paths in my PATH statement, or ~/Library/Python PATH references for Apple's /usr/bin/python3.


In your case, you might want just this PATH:


export PATH=".:/usr/local/bin:$PATH:/opt/homebrew/bin


I only use ~/.zshrc or ~/.zshenv files and not ~/.zprofile. I would get rid of that ~/.zprofile eval entry. I don't use it and specifically set a MANPATH in my ~/.zshrc without running any homebrew shellenv command.


export MANPATH="/usr/share/man:/usr/local/share/man:/opt/homebrew/share/man:/Library/Developer/CommandLineTools/SDKs/macOSX.sdk/usr/share/man:/Library/Developer/CommandLineTools/usr/share/man"







May 15, 2024 7:10 AM in response to ht0422

You're playing with fire there. Your post is pretty unusual in that you're using Homebrew and haven't come here because basic commands like "ls" or "cd" are no longer working. Just so you know, it seems like any OS updates can seriously corrupt Homebrew installations.


Otherwise, you appear to be doing it almost correctly. VikingOSX must be using an older version of the operating system. You definitely want those cryptex paths in there. That's part of the OS base path. Yeah, it makes the path look awful and overly complicated, but that's the way it is now.


That being said, you've still got Homebrew problems. Doesn't Homebrew have its own Python? The only improvement I can see is that you should add your custom paths before the previous path, like so:


export PATH=/Users/my_user/Library/Python/3.9/bin:$PATH


This way, your path would take precedence over a Homebrew path, or an Xcode Python path, or anything else.


You mentioned Jupyter too. The Jupyter/conda community has a practice of having lots of different versions of all those tools installed. In my experience, they typically have no idea what version of anything they are running, and everything is broken half the time. If you're going down that "path" (no pun intended), make sure you know exactly what is installed where and how your system is configured. That will be a challenge when Homebrew is involved.


PS: I should also mention an old technique that seems have become lost over time, but still works great. You can define various environment files that you source (using the "source" command) to change your environment. So if you want to run your custom version of Python, you could run "source mypython.env" to setup your environment. Instead of putting your export statement in the startup environment file, just move it to your custom environment file.

May 13, 2024 12:41 AM in response to ht0422

In Unix, the normal way to modify the PATH is not to mess with initialization scripts in system directories, but to add commands to modify the PATH in a shell initialization script associated with your user account. The default shell in Sonoma is zsh and you can read more about it by entering the command man zsh in Terminal.


Be careful when modifying initialization scripts – even "just" the ones in your own user account. If you mess up the setting of something like the PATH you can find yourself unable to execute basic commands like ls or rm unless you know the absolute path of the place where they are located.

May 15, 2024 8:59 AM in response to etresoft

If you look at /etc/paths, it includes Cryptex, and therefore, on macOS Sonoma 14.5, I don't expressly use any hardwired Cryptex paths in my PATH statement.


Homebrew has no Python installation unless it becomes a specific package installation dependency. In that case, and on Apple Silicon, Python 3.12.* is installed in /opt/homebrew/bin. Since I install the current Python3 from Python.org using their installer, it gets written into /Library/Frameworks/Python.framework and places relevant binaries in /usr/local/bin. That is why I always tack /opt/homebrew/bin onto the end of my PATH statement.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

PATH environmental valuable in Mac Sonoma 14.3

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