6

I have a script in my .bash_profile that prompts for X sessions to boot. When I launch tmux, I get this prompt that I had only intended for the TTY login.

Is there something I can put in .bash_profile that will simply run bash if it is part of tmux? That is, can I check with bash if .bash_profile is being read within tmux?

  • 3
    It appears that tmux starts login shells by default. I am not sure if there is a reason for this, but if you want to make tmux start non-login shells instead you can add set-option default-command bash to your .tmux.conf. (.bash_profile is only read by login shells; non-login shells ignore it). – jw013 Jan 18 '13 at 15:34
  • 2
    I asked a similar question and was redirected here. @jw013 's comment gives an effective way of changing this weird behaviour of tmux from within tmux (rather than tinkering the .*profile files), but it requires a -g flag to be effective, as in set -g default-command zsh – Dalker Oct 21 '13 at 21:34

3 Answers3

12

tmux sets an environment variable called $TMUX, which I believe holds the location of the socket it's using. Either way you can use it in your .bash_profile to test whether or not it is being called from within tmux.

if [ -z "$TMUX" ]; then
    # not in tmux, do non-tmux things
fi

Or

if [ -n "$TMUX" ]; then
    # called inside tmux session, do tmux things
fi
  • 1
    One could arrive at such knowledge by comparing 'printenv' output. – user1133275 Mar 28 '15 at 04:15
  • 2
    Is there any downside to this? I'm of the opinion that tmux should not launch shells as login shells, and while I've read vigorous debate about it elsewhere, I still haven't seen a compelling reason for it. I'm going to use this $TMUX check in my profile if for no other reason than to reduce $PATH duplication, but is there any downside? – Jim Stewart Oct 31 '16 at 17:47
4

I usually use $TERM to test that. screen and tmux set it to "screen" by default.

gelraen
  • 6,737
0

Instead of TMUX, you can export your own. It is useful in the case there are more than one tmux users and you want to source it only for yourself.

Add the following line to ~/.tmux.conf.

if-shell shell-command export SOMEONE_USING_TMUX=1

And, add the following lines to ~/.bash_profile.

if [[ ! -z "$SOMEONE_USING_TMUX" ]]; then
    # source for yourself
fi