5

I'm using Homebrew on Mac OS X 10.8.3. Homebrew wants the /usr/local/bin directory earlier in the PATH than /usr/bin, otherwise system-provided programs will be used instead of Homebrew managed replacements.

I'm using zsh, and in my .zshenv I reset the PATH, and then use path_helper to initialize it, like so:

if [ -x /usr/libexec/path_helper ]; then
  PATH=''
  eval `/usr/libexec/path_helper -s`
fi

Immediately following this, also in .zshenv, I prepend /usr/local/bin to the PATH.

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

There are various other additions to $PATH. RVM, /usr/local/sbin and my personal bin directory:

export PATH=$HOME/.rvm/bin :$PATH
...
export PATH=$PATH:/usr/local/sbin:$HOME/bin

Finally, I use typeset -u to remove any duplicates (although where they are coming from is a mystery to me) from the PATH.

typeset -U PATH 

After all of this here is what my PATH looks like:

/Users/mark/.rvm/gems/ruby-1.9.3-p374/bin
/Users/mark/.rvm/gems/ruby-1.9.3-p374@global/bin
/Users/mark/.rvm/rubies/ruby-1.9.3-p374/bin
/Users/mark/.rvm/bin
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin
/usr/local/sbin
/Users/mark/bin

I know that /etc/paths sets these paths:

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

so I edited that file and removed the /usr/local/bin' option so that the only place it is being set is in.zshenv`.

All of this is contained in my dotfile repository on GitHub (https://github.com/zan5hin/dotfiles), and is being used on two laptops. On the first laptop the path is correct, with /usr/local/bin immediately following the RVM entries. On the second laptop it appears as I detailed above.

I am at a loss to explain why the path is incorrect on the second machine when the zsh configuration is an identical copy.

Can anyone suggest why the path would be out of order?

Thanks.

Mark Nichols
  • 1,407
  • 2
  • 19
  • 25
  • By following the directions on this question (http://stackoverflow.com/questions/10343834/homebrew-wants-me-to-amend-my-path-no-clue-how) and editing the /etc/paths file to move `/usr/local/bin` to the head of the list, I was able to get both machines to have matching, and correct, paths. I still don't understand why exporting the path from my `.zshenv` didn't work as expected. – Mark Nichols Mar 31 '13 at 17:43

2 Answers2

5

zsh reads the files in the following order (from man 1 zsh)

  1. $ZDOTDIR/.zshenv
  2. /etc/zprofile (if login)
  3. $ZDOTDIR/.zprofile (if login)
  4. /etc/zshrc (if interactive)
  5. $ZDOTDIR/.zshrc (if interactive)
  6. /etc/zlogin (if login)
  7. $ZDOTDIR/.zlogin (if login)

If ZDOTDIR is unset, HOME is used instead. Files listed above as being in /etc may be in another directory, depending on the installation.

Your changes were to (1) which is before (2); the macOS default for (2) is:

% cat /etc/zprofile
# System-wide profile for interactive zsh(1) login shells.

# Setup user specific overrides for this in ~/.zprofile. See zshbuiltins(1)
# and zshoptions(1) for more details.

if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi

thus, your changes are being overridden by the macOS default. You will need to make your PATH changes later in the pipeline in order to preserve order.

Brandon LeBlanc
  • 562
  • 8
  • 12
0

This line is wrong:

export PATH=$HOME/.rvm/bin :$PATH

It should be:

export PATH=$HOME/.rvm/bin:$PATH

The space before :$PATH is causing you to lose the previous contents of $PATH.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Yes, it is wrong. Thanks for the catch. Correcting it however, has no effect on the original problem. Both laptops are using identical zsh configurations (via Git) and one has the correct path while the other doesn't. – Mark Nichols Mar 31 '13 at 17:05
  • 1
    Try putting `set -x` at the top of your .zshenv. Then it will display the commands as it executes them, and you can see how the PATH evolves. This should help you see where it's being messed up. – Barmar Mar 31 '13 at 20:43