10

Right now, when I log in to Tmux, only ~/.bash_profile gets loaded.

I'd like ~/.bashrc to get called instead.

Is that possible?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Kam
  • 5,878
  • 10
  • 53
  • 97
  • 3
    Possible duplicate of [.bashrc/.profile is not loaded on new tmux session (or window) -- why?](https://stackoverflow.com/questions/9652126/bashrc-profile-is-not-loaded-on-new-tmux-session-or-window-why) – Sylvain Leroux Jul 29 '17 at 12:49

2 Answers2

8

You can fix that at the tmux level by explicitly setting the default shell command:

tmux set-option default-command "/bin/bash"

From the tmux manual (emphasis mine):

         default-command shell-command
                 Set the command used for new windows (if not specified
                 when the window is created) to shell-command, which may
                 be any sh(1) command.  The default is an empty string,
                 which instructs tmux to create a **login shell** using the
                 value of the default-shell option.
         default-shell path
                 Specify the default shell.  This is used as the login
                 shell for new windows when the default-command option is
                 set to empty, and must be the full path of the exe‐
                 cutable.  When started tmux tries to set a default value
                 from the first suitable of the SHELL environment vari‐
                 able, the shell returned by getpwuid(3), or /bin/sh.
                 This option should be configured when tmux is used as a
                 login shell.

As explained by Chepner in a comment below:

default-shell defaults to your preferred login shell; default-command defaults to starting a login instance, effectively $SHELL -l

... and in the case of Bash, a login shell doesn't read ~/.bashrc. By overriding the value of default-command, we can now force tmux to create a non-login shell instead.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
  • You don't need the `-i` to start an interactive shell in this case, since the shell's standard input/error will be tied to a (pseudo)terminal. – chepner Jul 29 '17 at 13:04
  • @chepner Indeed; fixed in the answer. But I don't understand then why setting explicitly the default command to "/bin/bash" works but if I let the defaults it don't. Even while both my $SHELL and `passwd` entry are set to `/bin/bash`. According to the relevant part of the man quoted in my answer, it shouldn't make any difference. Or did I missed something? – Sylvain Leroux Jul 29 '17 at 16:25
  • 1
    `default-shell` defaults to your preferred login shell; `default-command` defaults to starting a login instance, effectively `$SHELL -l`. – chepner Jul 29 '17 at 16:39
  • Starting tmux with it in the command line does not work in XUbuntu 18.04 LTS & tmux 2.6. Isn't it meant to run in the command line? – Horror Vacui Sep 11 '20 at 06:59
7

This issue is not related to tmux. To solve it make sure to add source ~/.bashrc to .bash_profile and that's it.

You can learn more about bash initialization and which files it loads and in which order here: https://github.com/sstephenson/rbenv/wiki/Unix-shell-initialization#bash

As you can see .bashrc is not even on the list when bash is started in login mode, that's why we source it from the file (.bash_profile) that is on the list.

  • The Invocation section of the man page would be a better reference to which files are used on startup. – chepner Jul 29 '17 at 13:05
  • That doesn't work for me, i have already my `.bash_profile` sourcing my `.bashrc` and tmux isn't reading it – asa Jun 20 '21 at 10:50