4

My Linux terminal config files (.bash_profile, .profile, .bashrc) expand $PATH by prepending some custom directories. This only happens once (and I've also included some ENV vairiable based guards to enforce that it can only ever happen once). My .bash_profile also sources the .rvm script (~/.rvm/scripts/rvm) which prepends its own custom dirs. These RVM dirs must be first.

Everything's OK as long as I'm in a clean bash session.

If I run tmux, however, the directory entries from my config files get prepended to PATH doubly (regardless of my ENV variable guards). It seems that tmux has two environments for ENV variables which it then merges.

This is a problem, since entries prepended by the .rvm script get only prepended once, and in the tmux scenario they don't end up first.

How can I prevent this from happening?

Edit—additional info: All my PATH additions get prepended in .profile, which I include from .bash_profile (. ~/.profile). All my GUI terminals are run "as a login shell".

In each config file, I use guards of the following form to prevent double inclusion:

    if [ "$PROFILE_SOURCED" != "true" ]; then
         export PROFILE_SOURCED=true
    ...
    fi

By prepending an entry to PATH, I mean export PATH=entry:$PATH.

Petr Skocik
  • 28,816
  • Please explain which of those files is actually setting the path. .profile is ignored if .bash_profile exists and both are ignored when running non-login shells. Also see here for some tricks to make sure this does not happen. – terdon Apr 13 '14 at 15:02
  • @terdon I've expanded the question with some additional info. Thanks for looking into it. – Petr Skocik Apr 13 '14 at 15:29
  • Thanks but we'll need a bit more. How are you setting all your shells to be "login shells"? Can you actually show the relevant lines? Your test won't work since the variable will likely not always be set depending on how the shells are started. Did you read my previous link? There are much better ways of avoiding duplicates explained there. – terdon Apr 13 '14 at 15:33
  • I'm using the "Run command as a login shell option" in my terminal emulator (mate-terminal). (It's in RVM's manual at https://rvm.io/rvm/install). I'll try some of those tips in the link you gave. – Petr Skocik Apr 13 '14 at 15:48

2 Answers2

3

Solved by the following:

  1. Placed a guard around my PATH manipulation code in .profile

    if [ "$PATHS" != "true" ]; then
        export PATHS="true"
    #Manipulate and export PATH over here
    fi
    
  2. Removed file-level guards around .bash_profile and .bashrc

  3. IMPORTANT: RESTARTED the tmux server. (killall tmux) -- the manual indicates the server maintains its own environment, which it inherits from its parent shell. If config files change, tmux needs to start afresh.

Petr Skocik
  • 28,816
2

One possibility for getting duplicate entries on environmental variables is to have them recursively defined. Recursive definition occurs when you have, in ~/.bashrc, something of this sort :

export SOME_VARIABLE=$SOME_VARIABLE:/some/paths/

When tmux starts, it launches a login shell by default. You can change this behaviour by setting in ~/.tmux.conf the option

set-option -g default-command bash #or zsh, or whatever

as said in this question. Either way, tmux starts a shell, which in turn sources ~/.bashrc (either as non-login shell or as login shell, through ~/.profile). Since environmental variables are defined recursively in the .bashrc file, going through ~/.bashrc again will add /some/paths to those already defined paths. So they will be duplicated.

Many environmental variables are empty when a shell is launched. So defining them as

export SOME_VARIABLE=:/some/paths/

should do no harm and will prevent tmux from creating duplicates. If they start off with some values (like PATH), one solution might be to write down these defaults in the definition of the variable.