9

Suppose there are two terminals, pts/0 and pts/1.

In pts/0, I have fired a command:

ls>/dev/pts/1

Then, we only get the output of the ls command in pts/1, but I want to run a command as well as its output both on pts/1.

I tried "echo ls">/dev/pts/1, but instead the output of the ls command, "ls", is printed on pts/1.

How can I do it?

What I tried

2

3 Answers 3

6

Copy and paste this code on ttyecho.c

Compile it using gcc -O3 -o ttyecho ttyecho.c

Use it ./ttyecho -n /dev/pts/1 ls

2
  • 1
    @Tiger Be aware that the command string will first be parsed in the current terminal's shell but executed in the environment of the target's. You can see the difference with ttyecho -n /dev/pts1 ls $$ and ttyecho -n /dev/pts1 ls '$$'
    – Otheus
    Feb 7, 2016 at 7:47
  • There is also a repository with this tool. I have added a procedure how to check the correct escapement of the string to be sent to the other terminal: github.com/osospeed/ttyecho/issues/1 Jul 15, 2023 at 16:21
3

Depends on your terminal environment:

  • In Linux with the console: openvt _command_
  • Within a terminal in screen: screen _command_
  • Within X Windows (Mac as well): xterm -e "_command_"
1
  • For people reading this question, this is the right answer. Specifically, openvt is the correct way to do this on most Linux distributions
    – Max Coplan
    Oct 18, 2019 at 22:22
1

It's not clear to me why you want to do this; depending on the exact reason, the clusterssh package may do what you want.

The idea of clusterssh is that you start several ssh commands to several machines (or possibly the same machine multiple times). You then get a terminal window for each ssh session, plus one entry window of clusterssh. If you type something into one of the terminal windows, it gets sent only to that ssh session; if you type it into the main entry window, however, it gets sent to all sessions.

If that's not what you need, it might be useful if you could explain why you want to do this.

1
  • Yeah, clusterssh is a nice tool for working with a small set of hosts in parallel.
    – Otheus
    Feb 7, 2016 at 8:13

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