8

I've looked at this similar question which asks about creating a tmux session, or attaching if it doesn't exist.

What I'd like to do is a little different: create a tmux session if it doesn't exist, but don't attach. The plan is to create a few different sessions with canonical names, then send commands to them using tmux send. Is there an easy way in tmux or shell scripting to create a session with a specific name only if it doesn't exist?

Community
  • 1
  • 1
Thomas Johnson
  • 10,776
  • 18
  • 60
  • 98

1 Answers1

15

If you try to create a session with the same name tmux returns an error with exit code 1.

drizzt@liara ~ % tmux new-session -d -s test 
drizzt@liara ~ % tmux new-session -d -s test
duplicate session: test
drizzt@liara ~ % echo $?
1
drizzt@liara ~ % 

If you don't like the duplicate session error you can do something like:

drizzt@liara ~ % tmux has-session -t test || tmux new-session -d -s test
drizzt
  • 737
  • 1
  • 7
  • 14