Frage

I'm using Mac OS X, iTerm2, zsh and Tmux via Homebrew. When I start a Terminal session in iTerm2, the global PATH variable looks still fine. But when I open up a Tmux session the PATH variable is extended with the same paths it already consisted of. I'm going to put a issue solving code snippet in my .zshrc, but I'm still interested in the cause why the PATH variable is filled up twice.

War es hilfreich?

Lösung

This happens because your .zshrc is evaluated for every new zsh process. So when you start iTerm2 it gets evaluated making your changes to $PATH, then when you start tmux that gets your modified $PATH and passes it on to a new instance of zsh inside of there and that new zsh process again evaluates the .zshrc making the changes again.

There are several ways that you could prevent this.

$TMUX

First, to specifically prevent it from happening for shells inside of tmux you could skip making those changes if $TMUX is set:

if [[ -z $TMUX ]]; then
  PATH="$PATH:/foo"
fi

zprofile

Another option would be to move that portion of your .zshrc to your .zprofile file. This file is only evaluated by login shells. But, by default tmux starts new shells as login shells, so you'd also need to prevent tmux from doing that by adding the following to your tmux configuration:

set -g default-command /bin/zsh

You may need to adjust the path to zsh there. This would prevent tmux from starting zsh processes as login shells, so zsh inside of tmux wouldn't look at the .zprofile.

typeset

Another option somewhat along the lines of the code snippet that you linked to for preventing duplicates to be added would be to change your path modification to be something like:

typeset -aU path
path=( $path /foo )

This works because zsh automatically sets up the $path variable as an array that mirrors the content of $PATH. The -U option to typeset modifies that variable so that the entries are unique.

Andere Tipps

I found this GitHub thread very useful. The solution from this comment worked for me:

# /etc/zshenv
if [ -x /usr/libexec/path_helper ]; then
  PATH="" # Add this line
  eval `/usr/libexec/path_helper -s`
fi

By doing this, you'd have to put your PATH modifications in ~/.zshrc instead of ~/.zprofile. I also tried this solution from the thread but didn't work for me.

My solution:

Step 1:

In .bashrc or .zshrc

ExtraPath="/foo/bar:$HOME/bin" # your customized path here, /foo/bar and $HOME/bin for instance
if ! [[ "$PATH" =~ "$ExtraPath" ]] ; then PATH="$ExtraPath:$PATH" ; fi # if the PATH does not contain your customized path, then insert yours, else do nothing.

Step 2:

in ~/.tmux.conf, add

set -g default-command "${SHELL}"

in this case, tmux will not source /etc/profile, so it will not mess up with your PATH

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top