Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When I do 'open .profile' in the terminal I have the following:

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

Now I installed node.js for Mac and it says,

Make sure that /usr/local/bin is in your $PATH.

How can I add /usr/local/bin to export PATH=$PATH:/usr/local/git/bin?

like image 872
shin Avatar asked Jun 14 '12 02:06

shin


People also ask

Does Mac Have usr local bin?

Option #1. How to Go to /usr/local/bin on Mac via Terminal. You might be wondering where is Usr located. Well, you can get to the usr folder on your Mac by typing in a couple of commands on the Terminal.


1 Answers

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/bin 

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:

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 done 

And 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 done 
like image 135
Mark Reed Avatar answered Sep 23 '22 00:09

Mark Reed