10

This image shows how pseudo-terminals work:

enter image description here

If I have two terminal emulators open, I can send the string "Hello" from the first terminal emulator to the second terminal emulator:

echo "Hello" > /dev/pts/1

When this command is executed, the string "Hello" will travel from the slave side of the second pseudo-terminal to the master side of the second pseudo-terminal and then gets displayed on the second terminal emulator.

Now is it possible to send data in the opposite direction, that is, to send data to the shell instead of the terminal emulator (or the process that the shell is running)?

3
  • 1
    The TIOCSTI ioctl might be relevant (assuming you have root or the appropriate capability set)...
    – thrig
    May 15 '17 at 21:16
  • 1
    I think this question has been asked several times (can't find them ATM), and so answer so far has been "no easy way to fake input to the shell".
    – dirkt
    May 16 '17 at 7:20
  • 1
    Are you trying to run commands in the other terminal, or just dump text data there? The write, talk, and wall (as in "write to all") command line programs can do that.
    – Adam Katz
    Aug 31 '17 at 18:41
0

TIOCSTI works as said in a comment by thrig above: https://stackoverflow.com/a/29615101/2995591

0

It might not exactly be what you are looking for, but these kind of problems I usually solve by running screen sessions in my terminals.

I do it like this:

  • In terminal 1 I start a new screen session called term1 with screen -S term1
    Often when I need this functionality terminal 1 doesn't exist yet. In that case I create the session in a new terminal with xterm -e screen -S term1 & ; disown
    You might be wondering about the & ; disown. This is because I want the shell back after xterm has launched and I don't want xterm to die if the shell where I launched it dies
  • I do something similar in the 2nd terminal: screen -S term2 (or create a new terminal by running xterm -e screen -S term2 & ; disown)
  • Communication now works as follows:
    • You can send text to the 1st terminal from everywhere (including terminal 2) with a command like:
      screen -S term1 -X stuff 'echo "foo"\n'
      The result would be the same as entering echo "foo" at the 1st terminal and pressing Enter
    • Easier example: to send the input some words but not following it by Enter to terminal 2 you can use screen -S term2 -X stuff 'some words'

Sidenotes:

  • If you already had screen sessions named term1 and/or term2 use other names
  • If you ran screen -S somename in a already existing terminal note that only the screen session is killed after running exit.
    If you want the whole terminal to stop then use screen -S somename ; exit instead.
    This is not necessary in newly created terminals.
1
  • Note that running screen or tmux sessions in your terminals is almost always a good idea. For me the most important 3 reasons are the possibilities to: (de/re)-attach sessions, use multiple windows in 1 session, share sessions. Both tmux, screen and their frontends (e.g. byobu) also have a gigantic amount of other features. Check the manpages.
    – Garo
    Apr 21 at 6:28
-1

Each proccess has 3 default "file descriptors"

0 is stdin  (standart input)
1 is stdout (standart output)
2 is stderr (standart error output)

You need to find PID of shell process and then you can do following:

echo "test" > /proc/{your_shell_pid}/fd/1

This will print test in the shell process' standart output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.