18

Summary

When I create a new tmux session, my prompt pulls from a default bash configuration and I have to manually run source ~/.bashrc for my customized prompt.

Analysis

I am using a RHEL 7 machine. I began noticing this behavior after a bash update a while back, but haven't gotten around to asking the question until now (and am not sure which update this began happening around).

For example, I've customized my prompt to look like:

[user@hostname ~]$

Whenever I start a new tmux session, it uses what appears to be the bash default:

-sh-4.2$

A quick run of source ~/.bashrc always fixes the issue, but it's annoying that I have to do this every time I want to fix something small. Any ideas on how to get tmux to do this automatically again?

If any more information is needed, I am happy to provide.

tmux.conf

For reference, I have my tmux.conf file below, although it is hardly what you could call custom.

setw -g mode-keys vi

# reload tmux.conf
bind r source-file ~/.tmux.conf \; display-message " ✱ ~/.tmux.conf is reloaded"
20

As far as I know, by default tmux runs a login shell. When bash is invoked as an interactive login shell, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile. So you have to put source ~/.bashrc in one of those files.

Another way to solve this issue is to put in your file .tmux.conf the line:

set-option -g default-shell "/bin/bash"
  • 1
    I added the source ~/.bashrc line to my .bash_profile, tried logging out and back in, create a new tmux session, but no change. Any ideas? – Justin W. Flory Nov 8 '16 at 2:52
  • 1
    Try to put in your file .tmux.conf the line set-option -g default-shell "/bin/bash". This should solve your issue (at least I hope so)! – andreatsh Nov 8 '16 at 22:06
  • After reloading my tmux configuration, this solved my issue! Want to modify your answer or create a new one? I would be happy to mark it as accepted. :) – Justin W. Flory Nov 17 '16 at 4:24
  • You're kind, I quickly modified my answer! – andreatsh Nov 17 '16 at 15:08
  • 2
    Note for macOS users: if you've installed bash via Homebrew and this doesn't work, change "/bin/bash" to "/usr/local/bin/bash". – user278117 Apr 5 '18 at 14:48
7

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

Solution

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

  • This answer gave me the solution while the accepted one did not. – Javier Feb 26 at 16:24
3

Adding the following to .tmux.conf:

set-option -g default-shell "/bin/bash"

DOES NOT yield the desired result.

Only when adding source "$HOME/.bashrc" to ~/.bash_profile the intended result is achieved.

This will work on an active tmux session when opening a new window or pane, and also when detaching and opening a new tmux session.

Tested on:

VERSION="16.04.2 LTS (Xenial Xerus)"
tmux 2.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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