How to prevent Tmux from filling up the global PATH variable with duplicated paths?
Asked Answered
W

4

25

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.

Warfore answered 24/10, 2012 at 22:8 Comment(0)
P
38

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.

Paronymous answered 25/10, 2012 at 1:47 Comment(5)
Thanks, the $TMUX and typeset variants work perfectly in combination. Only RVM can't be handled on this way. I put [[ -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
Have you checked your .zprofile file? rvm seems to put code like that in there as well as in the .zshrc file. Also since you've got $path setup to be unique, you should be able to clean up duplicates by doing 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
In case of bash, i used 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
If the changes modified in your ~/.profile does not take effect, log out and log in, then it should workTuscany
D
3

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.

Disc answered 27/7, 2014 at 11:58 Comment(1)
solution didn't work for meKale
P
0

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

Pronation answered 6/6, 2021 at 10:30 Comment(0)
J
0

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)
Jiujitsu answered 15/6 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.