I recently started using tmux. I found I can attach-or-create a tmux session by using:

tmux new-session -A -s main

I was hoping to put this into .profile so that I automatically get put into tmux when I connect via SSH. I'd like to skip this when logging on locally.

I found this question which looks promising, however I'm concerned that if I launch tmux from .profile it'll then launch my shell and create another tmux ad infinitum!

So what's the correct way to avoid launching tmux from .profile if it's already running in tmux?


tmux sets the $TMUX variable to point to the socket, so you can do something like

if [ -z "$TMUX" ]

The stuff inside the test will only be run if the variable is not set - ie you're not already inside a tmux session.

  • Spot on - I dared to try this and got sessions should be nested with care, unset $TMUX to force. Thanks! – Danny Tuppeny Jul 4 '16 at 16:53

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.