Skip to content

Instantly share code, notes, and snippets.

@nunogt
Forked from Overbryd/path-in-mac-osx-10.7.mdown
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nunogt/0c7ed28b4cd0f79ed600 to your computer and use it in GitHub Desktop.
Save nunogt/0c7ed28b4cd0f79ed600 to your computer and use it in GitHub Desktop.
#!/bin/bash
PATHS_FILE="/private/etc/paths"
OSX_VERSION="$(/usr/bin/sw_vers -productVersion)"
if [ "$OSX_VERSION" == "10.10" ] ; then
patch -b $PATHS_FILE <<EOF
--- paths 2014-11-09 12:15:25.000000000 +0000
+++ paths.brew 2014-11-09 12:21:34.000000000 +0000
@@ -1,4 +1,5 @@
/usr/local/bin
+/usr/local/sbin
/usr/bin
/bin
/usr/sbin
EOF
else
patch -b $PATHS_FILE <<EOF
--- paths 2014-05-30 13:18:24.000000000 +0100
+++ paths.brew 2014-08-11 15:25:33.000000000 +0100
@@ -1,5 +1,6 @@
+/usr/local/bin
+/usr/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin
-/usr/local/bin
EOF
fi

$PATH in MacOSX 10.7

MacOSX has a truly global path setting that precedes any other setting like ~/.bash_profile. The file /private/etc/paths is a list of pathnames. The order from top to bottom defines the resulting order in the $PATH variable. After loading /private/etc/paths there is a directory /private/etc/paths.d/ with files in the same style. Those are appended to the $PATH variable.

The default content of /private/etc/paths looks like this:

/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin

The resulting $PATH variable looks like this:

$ echo "$PATH"
# => "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"

Homebrew

When using homebrew in its standard way, all your packages end up in /usr/local. This is a problem if you want to install software via homebrew that should replace system default installations.

Git is a good example.

The system default $ /usr/bin/git --version outputs git version 1.7.4.4. Your homebrew installed git (at the time of writing) $ /usr/local/bin/git --version outputs git version 1.7.8.3.

But without changing the default path combination, you end up using system Git instead of homebrew Git.

Correcting $PATH globally

There is various workarounds and fixes for this problem. For example one could tackle this problem for a specific application. TextMate for example allows you to set TM_GIT to a git executable of your choice. But why bothering with application specific settings, when you can fix the problem at its root.

I propose that /usr/local/bin comes before /usr/bin

Here are the contents of my corrected /private/etc/paths. I've moved /usr/local/bin on top.

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
@nunogt
Copy link
Author

nunogt commented May 30, 2014

The following one-liner is suitable to be pasted directly into the Terminal:

curl -s https://gist.githubusercontent.com/nunogt/0c7ed28b4cd0f79ed600/raw/24b08a17e2603e5c8e762cb211c060dcb5a1805b/homebrew-osx-paths.sh | sudo /bin/bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment