Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
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 at 15:02
    
@terdon I've expanded the question with some additional info. Thanks for looking into it. –  PSkocik Apr 13 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 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 rvm.io/rvm/install). I'll try some of those tips in the link you gave. –  PSkocik Apr 13 at 15:48

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.