26

I have a reproducible problem:

  1. set up my PATH in Bash .profile
  2. start tmux by tmux, tmux attach or any variant
  3. echo $PATH and see it with the same components but in different order

How to stop this? What explains it?

3 Answers 3

53

If you're on a Mac and have been wondering why /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin keeps getting prepended to PATH when you run tmux, it's because of a utility called path_helper that's run from your /etc/profile file.

You can't easily persuade tmux (or rather, bash) not to source /etc/profile (for some reason tmux always runs as a login shell, which means /etc/profile will be read), but you can make sure that the effects of path_helper don't screw with your PATH.

The trick is to make sure that PATH is empty before path_helper runs. In my ~/.bash_profile file I have this:

if [ -f /etc/profile ]; then
    PATH=""
    source /etc/profile
fi

Clearing PATH before path_helper executes will prevent it from prepending the default PATH to your (previously) chosen PATH, and will allow the rest of your personal bash setup scripts (commands further down .bash_profile, or in .bashrc if you've sourced it from .bash_profile) to setup your PATH accordingly.

Hope that makes sense...

6
  • 1
    This totally did it for me! I have it hidden behind an if [ -n "$TMUX" ] clause myself, but I was wondering -- how important is the [ -f /etc/profile ], really? Isn't it safe to assume that /etc/profile is always a regular file?
    – Ryan Lue
    Commented Jul 9, 2017 at 5:37
  • 1
    @RyanLue You could comfortably replace -f with -e, but I wouldn't personally try and source a file from a script that gets run when I login unless I'd checked it was there. I do tend to re-use my bash script on lots of different machines (and operating systems) though, so I like to make sure they're bullet proof. I can imagine some other unix variants might call it something else. Commented Jul 10, 2017 at 10:24
  • THANK YOU! This was driving me crazy, and putting that little snippet at the top of my ~/.bash_profile brought sanity back to my happy tmux world. Commented Oct 22, 2019 at 18:52
  • 5
    If you don't want to mess with system defaults, set -g default-command "${SHELL}" in .tmux.conf forces tmux to use non-login shells. I don't see the point in those anyway, since you usually open tmux after you've already logged in. Commented Jan 18, 2020 at 16:56
  • 1
    For zsh, your system may be using /etc/zprofile just a heads up to check
    – John
    Commented Aug 29, 2020 at 23:16
6

No; sorting $PATH would be a too crazy thing to do, since many systems depend on its user-set order.

However, tmux does start your shell in "login" mode, causing ~/.profile to be sourced again. This means that if you have something like PATH=/my/dir:/another/dir:$PATH in that file, it will be done again, resulting in $PATH containing /my/dir:/another/dir:/my/dir:/another/dir:(etc.). To avoid this, you could use another variable to check:

if [ "$_SKIP_PROFILE" ]; then
    return 0
else
    export _SKIP_PROFILE=y
fi

export PATH="/my/dir:/another/dir:$PATH"
1

@Graham Ashton Thanks for your idea

My suggestion would be that you put

if [ -f /etc/profile ]; then
    PATH=""
    source /etc/profile
fi

at your .zshrc file at the top of it.

MAKE SURE that your

export NVM_DIR="$HOME/.nvm"
. "/usr/local/opt/nvm/nvm.sh"

is below.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .