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.
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.
path=($path)
–
Paronymous path=($path)
is a good advice. Unfortunately, it doesn't solve the issue. I also rechecked my .zprofile and all other .z* files, but they have no rvm stuff in there. –
Warfore set -g default-command /bin/bash
and modified if [[ -z $TMUX ]]; then PATH="$PATH:/foo" fi
in my ~/.bash_aliases file. Worked for me, Thanks! –
Tanked 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
If the /usr/libexec/path_helper
is invoked by shell startup scripts (can be seen by running zsh --login -x
), it uses the PATH variable the tmux
started with.
To prevent it you can empty PATH variable when starting tmux
, because it will be built anyway by the shell inside tmux
:
PATH="" $(which tmux)
or make an alias
alias tmux='PATH="" '$(which tmux)
© 2022 - 2024 — McMap. All rights reserved.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
into the $TMUX if-block, but it (or something else) is still executed everytime I start a new Tmux session. So I always get duplicated RVM paths. – Warfore