Execute command in remote active terminal

commandfile-descriptorsstdoutterminal

Suppose you have a terminal emulator (T1) open with a PID of 6350.

From another terminal, type this command (C1):

echo "ls\n" > /proc/6350/fd/0

This writes ls and the new line in T1 but does not execute it. Why?

I also tried using cat|bash with echo "ls\n" > /proc/catid/fd/0 but it is still not executed.

How can I echo the command into another terminal and have the command executed?

possible answer :

$ mkfifo toto;
$ bash < toto;
$ echo "ls" > toto;

In this case you cannot write anymore directly in the terminal (everything is displayed the same way the command (C1) displayed thing in this terminal.

Best Answer

There is a command line utility called ttyecho that can send a command to another terminal (tty/pts) and have the command executed.

sudo ttyecho -n /dev/pts/5 ls

See: Utility to Send Commands or Data to Other Terminals (tty/pts)

Also see: ttyecho source code on github.

Another interesting tty command is selector, a real-time interactive pattern matcher in console that updates the tty input buffer.

# selector examples
selector -v -x @ <(find . -maxdepth 2 -type d | awk '{print $0"@cd "$0}')
selector -v -x @ <(grep -E -o 'http[^ ]+' fileWithURLS)

See: selector - DYNAMIC SEARCH IN CONSOLE