You can do this with tmux. When you start a session in terminalA, there will be an integer wrapped in braces in the bottom left corner of the window. This is the session id.
If you then launch terminalB, you can issue the command
tmux attach [id]
where id
is the number from terminalA, you will be able to control that terminal shell from either terminal.
However, if you do just the preceding steps, you will lose terminalA. There are a couple of convenient workarounds here. For one, if you wrap the attach
command, followed by && target-command
in parentheses, you can run them in a subshell. Theoretically, the result of those commands should have no effect on the environment of shellB. That said, when the result of that command is normally to destroy shellB, I could see it being hit or miss.
Another option is to run the command in a seperate process with the &
operator like so:
some-terminalB-command & tmux attach [id] && terminalA-command
This method I have a bit more confidence in. Still, we can do better:
some-terminalB-command & (tmux attach [id] && terminalA-command)
This uses both methods, so that the attach
is twice removed from shellB.