.bashrc not sourced - tmux and login shells

- Pranav Sivvam
Jan 3, 2025

problem

I noticed that my .bashrc was not getting sourced in tmux sessions.

root cause

Turns out that .bashrc is only sourced on non-login shells (see man bash). You can identify if a shell is a login shell with the following command:

echo $-
# if output is -bash then its a login shell, else non-login shell

The Issue: All my shells that tmux was spawning were login shells! Hence .bashrc was not sourced but rather /etc/profile, .profile were being sourced on each new terminal.

why this is problematic

See why that’s not recommended: https://www.gridbugs.org/daily/tmux-runs-a-login-shell-by-default/

TL;DR: It’s because things like variable expansion will get messed up. For example, $PATH is defined in .profile and usually it’s defined with itself being concatenated at the end:

export PATH=/bin:/sbin:${PATH}

So each time a new login shell is spawned, the PATH variable is concatenated making it longer and longer.

the fix

From https://www.gridbugs.org/daily/tmux-runs-a-login-shell-by-default/:

To have tmux run a non-login shell, add this to .tmux.conf:

set -g default-command "${SHELL}"

references