Lets say I have one terminal where the output of "tty" is "/dev/pts/2" From another terminal, I want to send a command to the first terminal and execute it. Using: echo "ls" > "/dev/pts/2" only prints "ls" in the first terminal Is there a way to execute the string?
5 Answers
No; terminals don't execute commands. They're just channels for data.
You can sort of run a command and attach it to another terminal like this, though:
ls </dev/pts/2 >/dev/pts/2 2>/dev/pts/2
It won't behave exactly like you ran it from that terminal, though, as it won't have that device set as its controlling terminal. It's reasonably close, though.
-
Yes, the thing executing commands is the shell, not the terminal. – Basile Starynkevitch Dec 30 '11 at 08:43
-
Thanks! didn't about just attaching the output to the terminal. It really helps. – Ameet Gohil Dec 30 '11 at 08:50
I realize it's a year late, but there is a simpler way I think. Doesn't this work?
ls > /dev/pts/2
It works on my system.

- 11,455
- 3
- 39
- 62

- 11
- 1
-
This works for `ls`, because it doesn't accept input and usually doesn't display error messages. It won't work for more complex commands, though. – Oct 29 '14 at 18:49
You could execute /bin/sh > /dev/pts/5 to get a shell in the pty, then execute other commands. If you want a true shell functionality, you could maybe implement a thread (in a C program, with the openpty commands and all) to read the master pty and output its contents.

- 1
- 1
Usually getty, login, and shell programs are needed for executing commands from tty.
But you can also put a shell directly executing commands from a pseudo terminal. This is simplified example (all error checkings removed):
int main( int argc, char** argv )
{
int master_fd = create_my_own_psudo_terminal() ;
// Wait until someone open the tty
fd_set fd_rset;
FD_ZERO( &fd_rset );
FD_SET( master_fd, &fd_rset );
select( master_fd + 1, &fd_rset, NULL, NULL, NULL );
dup2( master_fd, STDIN_FILENO );
execl("/bin/sh", "sh", 0 );
return 0;
}
Now you can do the following:
Start this simple program in the first terminal.
And send your command from the second terminal:
echo "ls" > /dev/pts/5
And you will get listing in the first terminal.
Note: This is quite unsecure, because login is not done.

- 8,007
- 2
- 26
- 57