106

When tmux starts or opens a new window, it does not load my .profile or .bashrc. I end up typing . ~/.bashrc every time. Is there a way to make this happen automatically?

isomorphismes
  • 8,233
  • 9
  • 59
  • 70
John Smith
  • 1,813
  • 4
  • 15
  • 18

6 Answers6

162

Yes, at the end of your .bash_profile, put the line:

. ~/.bashrc

This automatically sources the rc file under those circumstances where it would normally only process the profile.

The rules as to when bash runs certain files are complicated, and depend on the type of shell being started (login/non-login, interactive or not, and so forth), along with command line arguments and environment variables.

You can see them in the man bash output, just look for INVOCATION - you'll probably need some time to digest and decode it though :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 2
    The problem with this solution is that on some systems, .bashrc calls .bash_profile. – Honza Jun 01 '13 at 17:30
  • 1
    Let me correct myself: systems do not have .bashrc calling .bash_profile, but you need to make sure you're not doing yourself. – Honza Jun 01 '13 at 17:37
  • 1
    @Honza, that would be a most unusual setup since it's almost always the other way around. Someone could certainly arrange to do it that way but we'd have to question their sanity :-) – paxdiablo Jun 02 '13 at 03:07
  • 2
    Indeed, it turned out that I have arranged to do it that way (and forgot about it). You're welcome to question my sanity... ;) – Honza Jun 23 '13 at 08:46
  • what about tmux under Linux? I want to run `gnome-terminal -e tmux` so that tmux automatically starts when I run the command, but neither my `.profile` nor my `.bashrc` are sourced. (`.profile` is automatically sourced when `gnome-terminal` is run, however this is not the case when a `tmux` is passed in after `-e`.) Any way to fix this ion Linux? `gnome-terminal -e "tmux new bash"` doesn't work and `.bashrc` is sourced in `.profile` as it should be - as I said `.profile` is automatically sourced when run normally. – dylnmc May 01 '15 at 06:51
  • @dylnmc, if you have a different question (while related, it does seem to be different enough to warrant difference), it's probably better to ask it as a new question. That way, you'll get more exposure. – paxdiablo May 02 '15 at 13:08
  • 3
    I thought that was the question... `.bashrc/.profile is not loaded on new tmux session (or window) — why?` – dylnmc May 07 '15 at 01:49
  • @dylnmc Under Linux I created a `~/.bash_profile`, included `. ~/.bashrc` and setup my gnome-terminal to run `tmux attach` (session is created in tmux.config) and it is working. Take a look at the image from this answer: http://stackoverflow.com/a/29794590/1814970 – marcelocra Jul 09 '15 at 01:09
  • It do seem to scramble my vim syntax highlighting. – marcelocra Jul 09 '15 at 02:06
  • @infostacker I saw that later on. I actually answered that a few answers down :) – dylnmc Jul 09 '15 at 07:02
22

Running bash explicitly worked for me, by adding this line to my ~/.tmux.conf file:

set-option -g default-command "exec /bin/bash"
Keith
  • 221
  • 2
  • 2
  • 3
    This worked for me! However, you don't need the "exec" part. I added the following line to my `~/.tmux.conf `: `set -g default-command "/bin/bash"`. Thanks mate. – joker Apr 21 '19 at 15:14
  • But, do you have any idea why is this happening? I suddenly noticed this after upgrading from Ubuntu 16.04 to 18.04.2. This is the only difference I can think of. Also, I noticed that some of the configurations broke and I had to comply to some new syntax (seems like tmux got updated as well, but I don't remember the tmux version I previously used to verify). – joker Apr 21 '19 at 15:19
14

Former answers provided solutions but didn't explain the reason. Here it is.

This is related to the Bash init files. By default, ~/.bashrc is used in an interactive, non-login shell. It won't be sourced in a login shell. Tmux uses a login shell by default. Hence, shells started by tmux skip ~/.bashrc.

default-command shell-command

The default is an empty string, which instructs tmux to create a login shell using the value of the default-shell option.

Init files for Bash,

  1. login mode:
    1. /etc/profile
    2. ~/.bash_profile, ~/.bash_login, ~/.profile (only first one that exists)
  2. interactive non-login:
    1. /etc/bash.bashrc (some Linux; not on Mac OS X)
    2. ~/.bashrc
  3. non-interactive:
    1. source file in $BASH_ENV

The weird interactive, non-login loading requirement confuses people in other situations as well. The best solution is to change the loading requirement of ~/.bashrc as interactive only, which is exactly what some distros, like Ubuntu, are doing.

# write content below into ~/.profile, or ~/.bash_profile

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

This should be the solution you desire. And I recommend every Bash user setup this in the profile.

References

Simba
  • 23,537
  • 7
  • 64
  • 76
  • 1
    that was an excellent explanation and finally helped me after reading a bunch of things trying to understand all this, thank you – XRBtoTheMOON Apr 24 '21 at 16:11
1

The solution that worked for me is the following:

  • Create a .bash_profile file if you don't have one in ~
  • At the end of .bash_profile put source ~/.bashrc or source ~/.profile
  • Restart tmux.

The issue should now be fixed.

Jaco Malan
  • 11
  • 1
1

From this thread:

seems using .bash_profile would work.

icyrock.com
  • 27,952
  • 4
  • 66
  • 85
  • The link just gives the advise to use: `$echo case $- in *i*) . ~/.bashrc;; esac >> .bash_profile` – Kjell Jul 02 '18 at 21:55
0

I had the same problem and the solutions so far didn't work for me. The solution that ended up working for me can be found here.

In short, tmux windows/sessions use a login shell which looks for a ~/.profile among other files when it starts.

What I wanted was to have zsh start with each new tmux window so I put exec zsh at the bottom of my ~/.profile.

Kellie Lutze
  • 64
  • 1
  • 4