Suppose the following scenario. I have machines named host1 through host4 to which I want to connect in one pane each.

I have split my Tmux window evenly into four panes so each of them can host an SSH connection to those hosts.

All is fine when already connected and having the panes synchronized. I can issue commands which get executed on all of the hosts simultaneously.

However, now I was thinking that it would be quite nice to be able to connect to each host by issuing the exact same command. This way I could enable synchronization between panes even before connecting to those hosts.

Alas, I am unable to come up with a method that works. What I tried was this:

ssh root@host$(tmux run "echo '#{pane_index}'")

The idea here being that this should resolve to ssh root@host1 through ssh root@host4 depending on the pane in which the command gets to run.

Unfortunately the result was rather unexpected. Even though the command executed (while the pane 1 was active), the connections were all established to host1. So evidently that variable pane_index refers to the currently active pane rather than the pane in which the command is being issued.

How can I achieve connecting to host1 through host4 making use of the pane index (or something else) by issuing the exact same command in each pane, rather than having to type individual commands?

NB: all indexes start at 1, but of course this could be made to work even if that weren't the case, e.g. by means of $((...)) in Bash.


You can use the variable $TMUX_PANE that tmux places in the environment of each pane's command. For example if you give the command

tmux send-keys  'echo host${TMUX_PANE#%}' Enter

you will see in each pane host0, or host1, and so on. So replace echo by your ssh command.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.