Home > Software engineering >  What is the right file where add search paths to the zsh enviroment variable `PATH` on macOS
What is the right file where add search paths to the zsh enviroment variable `PATH` on macOS

Time:12-01

I just compiled Ruby from source and it is located into /usr/local/ruby

In order to access Ruby's executables I edited ~/.zshenv adding /usr/local/ruby/bin to the export PATH directive:

export PATH=/usr/local/ruby/bin:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:$PATH
            ^^^^^^^^^^^^^^^^^^^^

However restarting the terminal and running which ruby still returns macOS's default /usr/bin/ruby

In fact inspecting PATH reveals:

% echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/usr/local/ruby/bin:/usr/local/mysql/bin:/usr/local/sbin:

So after my ~/.zshenv is excecuted another configuration file prepends /usr/bin to PATH.

Where does this happen?

I would expect to find /usr/bin already in PATH when .zshenv is processed (resulting in having this path at the end of the environment variable).

What I am missing?


I checked and there are no other zsh configuration files in my home directory, just .zshenv;

I checked /etc too and found

zprofile
zshrc
zshrc_Apple_Terminal

but none of those do alter the PATH variable


on /etc/paths

I have

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

but again, shouldn't be PATH already set with those paths when .zshenv is processed ?

CodePudding user response:

I would not trust apple to leave your /etc/paths directory alone, it's in their scope. Unless you have a need to manage this on a multiple person level, I would stick to changes in ~. For quick impact, I just add exports to .zshrc, and this is where I have my rvm bin directory added to my path.

CodePudding user response:

The right place to alter PATH by appending user's search directories is ~/.zshrc that is sourced last.

This solution is based on the answers and comments to this question, other answers and comments to my question.

 


 

Following @Philippe advice I noticed actually .zshenv is sourced first, before any other configuration file/script.

After reading a post on the Zsh Mailing List Archive I put my PATH setting directives into ~/.zpath

Then ~/.zpath has to be sourced from ~/.zshenv when not in a login shell:

if [[ $SHLVL == 1 && ! -o LOGIN ]]; then

source ~/.zpath

fi

And from ~/.zprofile that is executed only when in a login shell.

source ~/.zpath


Alternatively o̲n̲ ̲m̲a̲c̲O̲S̲ it possible to just edit directly /etc/paths

/usr/local/mysql/bin

/usr/local/ruby/bin

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

/usr/bin

/bin

/usr/sbin

/sbin

  • Related