How can I tell if I'm in a tmux session from a bash script?

31,671

Solution 1

Tmux sets the TMUX environment variable in tmux sessions, and sets TERM to screen. This isn't a 100% reliable indicator (for example, you can't easily tell if you're running screen inside tmux or tmux inside screen), but it should be good enough in practice.

if ! { [ "$TERM" = "screen" ] && [ -n "$TMUX" ]; } then
  PS1="@$HOSTNAME $PS1"
fi

If you need to integrate that in a complex prompt set via PROMPT_COMMAND (which is a bash setting, by the way, so shouldn't be exported):

if [ "$TERM" = "screen" ] && [ -n "$TMUX" ]; then
  PS1_HOSTNAME=
else
  PS1_HOSTNAME="@$HOSTNAME"
fi
PROMPT_COMMAND='PS1="$PS1_HOSTNAME…"'

If you ever need to test whether tmux is installed:

if type tmux >/dev/null 2>/dev/null; then
  # you can start tmux if you want
fi

By the way, this should all go into ~/.bashrc, not ~/.bash_profile (see Difference between .bashrc and .bash_profile). ~/.bashrc is run in every bash instance and contains shell customizations such as prompts and aliases. ~/.bash_profile is run when you log in (if your login shell is bash). Oddly, bash doesn't read ~/.bashrc in login shells, so your ~/.bash_profile should contain

case $- in *i*) . ~/.bashrc;; esac

Solution 2

As for previous answers, testing the ${TERM} variable could lead to corner cases, tmux sets environment variables within its own life:

$ env|grep -i tmux
TMUX=/tmp/tmux-1000/default,4199,5
TMUX_PANE=%9
TMUX_PLUGIN_MANAGER_PATH=/home/imil/.tmux/plugins/

In order to check if you're inside a tmux environment, simply check:

$ [ -z "${TMUX}" ] && echo "not in tmux"

Solution 3

If you're running tmux release 3.2 or later (or using OpenBSD 6.8 or later, or built tmux from sources newer than May 16 2020), you may use the TERM_PROGRAM environment variable.

if [ "$TERM_PROGRAM" = tmux ]; then
  echo 'In tmux'
else
  echo 'Not in tmux'
fi

Earlier releases of tmux does not have this environment variable.

Solution 4

After trying different ways, this is what ultimately worked for me, in case it helps anyone out there:

if [[ "$TERM" =~ "screen".* ]]; then
  echo "We are in TMUX!"
else
  echo "We are not in TMUX :/  Let's get in!"
  # Launches tmux in a session called 'base'.
  tmux attach -t base || tmux new -s base
fi

In this code snippet, I check to see if we're not in TMUX environment, I launch it. If you put this code snippet in your .bashrc file, you will automatically run TMUX anytime you open terminal! P.S.: tested on Ubuntu shell.

Share:
31,671

Related videos on Youtube

Brant
Author by

Brant

Updated on September 18, 2022

Comments

  • Brant
    Brant over 1 year

    I like to keep my bash_profile in a git repository and clone it to whatever machines I have shell access to. Since I'm in tmux most of the time I have a user@host string in the status line, rather than its traditional spot in the shell prompt.

    Not all sites I use have tmux installed, though, or I may not always be using it. I'd like to detect when I'm not in a tmux session and adjust my prompt accordingly. So far my half-baked solution in .bash_profile looks something like this:

    _display_host_unless_in_tmux_session() {
        # ???
    }
    export PROMPT_COMMAND='PS1=$(_display_host_unless_in_tmux_session)${REST_OF_PROMPT}'
    

    (Checking every time probably isn't the best approach, so I'm open to suggestions for a better way of doing this. Bash scripting is not my forte.)

  • StevieD
    StevieD about 5 years
    [ "$TERM" = "screen" ] may not work. In my case, my screen was reporting as screen-256 color.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 5 years
    @StevieD I don't think tmux does this on its own, but it might be a distribution patch or configuration.
  • CodyChan
    CodyChan over 3 years
    $TMUX and $TMUX_PANE don't mean "in a tmux session", they just mean they exist, if you run a terminal running with tmux, open another terminal running without tmux, check their values in second terminal, they still exist.
  • Kusalananda
    Kusalananda over 2 years
    Wouldn't this code also think we're in tmux if we were using GNU screen?
  • oalders
    oalders over 2 years
    My TERM var is a reflection of what's in my tmux config. In my case set -g default-terminal "tmux-256color", so TERM=tmux-256color is what is set in my env.