Score:1

Run command on another exist terminal

ru flag

Suppose there are two terminals 0 and 1. What command should I type to terminal 0

to make terminal 1 execute command.

Known: {command} > /dev/pts/1 does not work. command is executed on terminal 0

and redirect result to terminal to terminal 1.

Cas avatar
in flag
Cas
Why would you want that? It doesn't matter in which terminal you execute a command. You'll get the same result, so why would you want it in two terminals? And why run the command from terminal 0 and execute it in terminal 1, while you could also just execute the command in terminal 0.
王柏翔 avatar
ru flag
Because I want to get information about terminal 1, such as pwd, history,...
Nate T avatar
it flag
There is only one history for all bash instances, located at `~/.bash-history`, and even that is touchy. It likes to pick and choose at which commands it saves
Nate T avatar
it flag
Yea, you need tmux. Your issue is the reason tmux was created. The sessions came first, before the splits, etc. just to solve the problem of multiple users needing to access the same shell environment at the same time but from different locations.
Score:0
it flag

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.

王柏翔 avatar
ru flag
Thank for your suggestion. I try to know about tmux, i think that tmux can get the information between tmux session. However, I want to receive information about terminal whenever it starts, not only these terminals using the tmux command.
Nate T avatar
it flag
Sry, that is why we are always pushing for more context in the question. The more context you add (e.g. why you are trying to do x, any related details) the less likely it is to see answers that aren't helpful and the more likely it is that you will get helpful 'out of the box' answers that look at the whole situation from a different angle and solve the problem without answering the question directly. Sometimes there is no direct answer, but there is always a solution. : ) Ill see if I can't come up with something else for you.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.