Is there a way, being outside of tmux, in a specific session to get a window index by window name provided that there is
either 0 or 1 window with a given name?
I need this info to prevent creation of windows with the same name,
So I either get a window index to attach to or if there is no windows with the given name, create one.


You can get this information with tmux list-windows. To list all windows in all sessions use the -a flag or specify a session id with -t.

If you want the window ID you would use something like:

tmux list-windows -F '#I "#W"' | awk '$2 ~ /"NAME"/ { print $1 }'

If you only want to know if it exists:

if tmux list-windows -F '#W' | grep -q "^$NAME\$"; then
    echo $NAME exists
    # create new window ...

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.