Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 13 down vote accepted

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
    source /etc/profile

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...

share|improve this answer

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
    export _SKIP_PROFILE=y

export PATH="/my/dir:/another/dir:$PATH"
share|improve this answer

You must log in to answer this question.

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