macos – How to add /usr/local/bin in $PATH on Mac

macos – How to add /usr/local/bin in $PATH on Mac

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:

n

export PATH=$PATH:/usr/local/git/bin:/usr/local/binn

n

That 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:

n

path+=(/usr/local/git/bin /usr/local/bin) n

n

In either case, you may want to check to make sure the directory isnt already in the PATH before adding it. Heres what that looks like using the generic syntax:

n

for dir in /usr/local/git/bin /usr/local/bin; don   case $PATH in n     $dir:*|*:$dir:*|*:$dir) :;; # already there, do nothingn     *) PATH=$PATH:$dir          # otherwise add itn   esacndonen

n

And heres a zsh-specific version:

n

for dir in /usr/local/git/bin /usr/local/bin; don  if (( ${path[(i)$dir]} > $#path )); thenn    path+=($dir)n  findonen

Try placing $PATH at the end.

n

export PATH=/usr/local/git/bin:/usr/local/bin:$PATHn

macos – How to add /usr/local/bin in $PATH on Mac

To make the edited value of path persists in the next sessions

n

cd ~/ntouch .bash_profilenopen .bash_profilen

n

That will open the .bash_profile in editor, write inside the following after adding what you want to the path separating each value by column.

n

export PATH=$PATH:/usr/local/git/bin:/usr/local/bin:n

n

Save, exit, restart your terminal and enjoy

Leave a Reply

Your email address will not be published.