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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
I usually use $TERM to test that. screen and tmux set it to “screen” by default.
Method 3
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0