The PATH variable holds a list of directories separated by colons, so if you want to add more than one directory, just put a colon between them:
export PATH=$PATH:/usr/local/git/bin:/usr/local/binThat syntax works in any Bourne-compatible shell (sh, ksh, bash, zsh...). But zsh, which is the default shell in recent versions of MacOS, also exposes the PATH another way - as a variable named (lowercase) $path, which is an array instead of a single string. So you can do this instead:
path+=(/usr/local/git/bin /usr/local/bin)In either case, you may want to check to make sure the directory isn't already in the PATH before adding it. Here's what that looks like using the generic syntax:
for dir in /usr/local/git/bin /usr/local/bin; do case "$PATH" in $dir:*|*:$dir:*|*:$dir) :;; # already there, do nothing *) PATH=$PATH:$dir # otherwise add it esac doneAnd here's a zsh-specific version:
for dir in /usr/local/git/bin /usr/local/bin; do if (( ${path[(i)$dir]} > $#path )); then path+=($dir) fi doneI'm trying to install brew but get the following warning:
Warning: /usr/bin occurs before /usr/local/bin This means that system-provided programs will be used instead of those provided by Homebrew. The following tools exist at both paths: 2to3 2to3-2.7 idle idle2.7 pydoc pydoc2.7 python python-config python2.7 python2.7-config pythonw pythonw2.7 smtpd.py smtpd2.7.py Consider amending your PATH so that /usr/local/bin is ahead of /usr/bin in your PATH.I have no idea how to amend the path. I've explored several files from this stack overflow post (//stackoverflow.com/questions/8886114/using-brew-with-ruby-1-9-2), but can't find a relevan path to edit. The only file that I do have that exists is my /etc/profile file which currently looks like:
# Setting PATH for Python 2.7 # The orginal version is saved in .bash_profile.pysave PATH="/Library/Frameworks/Python.framework/Versions/2.7/local/bin:${PATH}" export PATHI wanted to just uninstall python then reinstall it through homebrew, but am a little nervous about breaking too many things at once.
Is there an easy way to amend the path? Should I create a .bash_profile file? Thank you for any help in advance.
By August 18, 2022August 19, 2022
macos – Making sure /usr/local/bin is in my $PATH on mac
echo $PATH will print your path. If you see /usr/local/bin between some colons, then its in your path.
open terminal and type the command below
echo $PATHYou should see something like this
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/binthe presence of /usr/local/bin in the output means you are good to go
macos – Making sure /usr/local/bin is in my $PATH on mac
Related posts on Macos :
- macos – Java/JDK for the Apple M1 chip
- macos – OpenSSL and Rand_bytes
- macos – Setup local LDAP server in mac OSX
- macos – Remote debugging using lldb/Xcode
- macos – Gulp command not found after install
- How do I get out of macOS X Utilities screen?
- How do you get to macOS Utilities screen?
- Does macOS High Sierra have Bluetooth?
- macos – How to install pdftk on Mac OS X
Post navigation
In newer macOS versions custom executables belong in the directory /usr/local/bin. However, the path /usr/local/ is not included in the system’s default PATH variable. This article shows you how to add /usr/local/bin to the system PATH variable on macOS. Since macOS El Capitan you cannot put any custom files under /usr/bin/. Even when you sudo as hard as you can (with
administrator rights) you cannot touch the files in that directory. Therefore, you are forced to use /usr/local/bin for your custom executables. Also if you are using Homebrew to install programmes on your system, the programme executables will be installed under the path /usr/local/bin. However, the path /usr/local/ is not included in the system’s default PATH variable so you cannot e.g. use these executables from within your Java programmes. As a consequence, you most-probably want to add
path /usr/local/bin to the system PATH environment variable to add the executables to your default executables. Of course, you can easily add /usr/local/bin to the PATH environment variable by editing the profile file in ~/.profile and add the expression: export PATH=/usr/local/bin:$PATH But does this really add the executables under the path /usr/local/bin to the default executables? The answer is no, it does not. For
example, consider you are running a Java application that executes some system executables/programmes myex located in /usr/local/bin using the Runtime object: Runtime.getRuntime().exec("myex")Why not use the profile file?
The code above would result in an error because the default path configuration does not include /usr/local/bin because the default path configuration is usually something like /usr/bin:/bin:/usr/sbin:/sbin.
Changing the Java code
One workaround may be to change the Java code and explicitly source the .profile file containing the aforementioned export statement:
Runtime.getRuntime().exec("source ~/.profile; myex")
Another workaround may be to call the executable using its absolute path /usr/local/bin/myex:
Runtime.getRuntime().exec("/usr/local/bin/myex")
Both workarounds would make strong assumptions either about the contents of the profile configuration or about the location of the executable.
As a result, the more elegant solution is to add the executable myex to the default executable path. This comment on github provides a way to achieve that. Open a Terminal window and copy & paste the following command:
sudo launchctl config user path /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
The command will set the default environment and add /usr/local/bin to the PATH variable of the system’s default environment. Hence, the executables under /usr/local/bin are callable from the default configuration.