Bash – Prevent tmux from starting up on SSH

bashsshtmux

I would like tmux to create a new-window when I ssh onto a machine from an existing tmux session. However, I do not want a tmux session started on the new machine!

I have the following in my .bashrc, so that tmux automatically starts up:

if [[ "$TERM" != "screen" ]]
then
   # try to attach to existing session, or start a new one
   tmux attach-session -t "$USER" || tmux -2 new-session -s "$USER"
   exit
fi

I also have an ssh function:

alias ssh='ssh_func'
ssh_func (){
    if [[ "$TERM" == "screen" ]]; then
        tmux new-window -n "$1" "ssh $@";
    else
        /usr/bin/ssh "$@";
    fi
}

This works ok, but I do not want a tmux session started on the machine I ssh to, because this gives me 2 sessions in the same terminal window. Is there anything I can put in my .bashrc so that tmux does not start up on a machine if the ssh command has been invoked from a tmux session?

I am using PuTTY and tmux 1.5.

Best Answer

Given the code you've posted, if you run ssh from within tmux, you'll have $TERM = screen, so you won't be trying to attach to a tmux window. In other words, the code you already have should work as desired. There's something fishy going on. Make sure your dot files don't mess up the TERM variable (if you need to modify TERM, which is very rare, make sure to do it only in very specific circumstances; in particular don't change it if it's screen).

Related Question