I'd love to be able to write a script that checked for the presence of a tmux session and attach it, or if no session exists, start a new one. So that I could create a custom command in my .zshrc file, and not have to always check.

Is there a way to check programmatically that I have a tmux screen session running?

2 Answers 2


There is an answer over on StackOverflow https://stackoverflow.com/questions/3432536/tmux-create-session-if-none-exists

tmux attach || tmux new

tmux ls will list running sessions

If you're using this inside a .shrc file or similar with exec I'd recommend

if tmux ls
  exec tmux attach
  exec tmux

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.