why can't I write to the standard input of my terminal device from another terminal

Viewed 1887

I have two pts terminals open in my Gnome desktop manager Ubuntu.

What I am trying to do is to write something to the terminal /dev/pts/0 using the terminal /dev/pts/1 using redirection like:

##in pts/1
echo date > /dev/pts/0   

But in pts/0, only date is simply printed and pressing enter doesn't execute it. So i guessed the comamnd is not going to the standard input of pts/0 .So I tried piping the output of echo date to /dev/pts/0 which gave me permission denied error to which i became root and changed permissions of it and still i couldn't get date command to run in pts/0 .

I am trying these things for learning purposes. So i really am confused how its all working here and what i should do to get it done.

2 Answers

Writing to a terminal device just prints output on the terminal. If it stuffed the text back into the input buffer, then everything you printed to stdout would loop back into stdin, since they're both connected to the same terminal device.

In order to put data into a pseudo-tty's input buffer, you have to write to its master device. Unfortunately, they don't have distinct names in the filesystem on Linux. There's a single /dev/ptmx device, and the master process uses grantpt() to create a slave that's linked to it before spawning the child that uses it as its controlling terminal. So there's nothing in the filesystem that you can write to that will feed into the pty's input buffer.

You can do it by doing this commands, (from /dev/pts/1 or another tty):

exec 1>/dev/pts/0

to deactivate

exec 1>/dev/pts/1 #or your actually original tty address.

Basically you are supplanting the tty stdin.

Edited for more details.

"exec" in this case starts a new bash and you can feed this with a new set of environment variables that normally you can not change on the fly. For more details please do "man exec".

"1>/dev/pts/0" here we are saying, "whatever I type on this new bash, write it to this another one, and indeed it will do it, but all the stdout will be displayed at the original tty.

Good luck learning linux, I hope you enjoy it.

Related