9

I'm trying to determine if a tmux session exists by has-session command.

If I have a session named abcdf:

adm@VirtualBox:/$ tmux -V
tmux 2.1
adm@VirtualBox:/$ tmux ls
abcdf: 1 windows (created Fri Feb  3 16:19:20 2017) [237x56] (attached)

has-session command will return the session named a,ab,abc,abcd,abcdf is exist (error code=0)

adm@VirtualBox:/$ tmux has -t a
adm@VirtualBox:/$ echo $?
0
adm@VirtualBox:/$ tmux has -t ab
adm@VirtualBox:/$ echo $?
0
adm@VirtualBox:/$ tmux has -t abc
adm@VirtualBox:/$ echo $?
0
adm@VirtualBox:/$ tmux has -t abcd
adm@VirtualBox:/$ echo $?
0
adm@VirtualBox:/$ tmux has -t abcdf
adm@VirtualBox:/$ echo $?
0
adm@VirtualBox:/$ 

and the following is not exist (error code=1)

adm@VirtualBox:/$ tmux has -t b
can't find session b
adm@VirtualBox:/$ echo $?
1
adm@VirtualBox:/$ tmux has -t bc
can't find session bc
adm@VirtualBox:/$ echo $?
1
adm@VirtualBox:/$ tmux has -t bcd
can't find session bcd
adm@VirtualBox:/$ echo $?
1

It seems like this command will match the sessions by prefix-matching, is this correct? How do I search the session exact the name I want?

3
  • tmux list-session and parse it with egrep that is more controllable in term of parsing?
    – Alex
    Feb 3, 2017 at 13:37
  • @Alex Yes, I currently use tmux ls -F "#{session_name}"|grep -Fx $session >/dev/null to search, but I'm still curious about this behavior since its manpage says: Report an error and exit with 1 if the specified session does not exist.
    – ytch
    Feb 5, 2017 at 1:28
  • I agree that mapage has too broad description and leave a space for assumptions that's why I switched to parse list-session too
    – Alex
    Feb 5, 2017 at 2:50

1 Answer 1

12

The solution is to use = e.g.

-t=sessionname

This does an exact match.

(Having the same issue today and found via: https://github.com/tmux/tmux/issues/346)

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .