In Linux, a Terminal is associated with a Shell. The Terminal send input to the Shell (for example: pwd
), and the Shell send the output back to the Terminal (for example: /home/paul
).
This diagram shows the relationship between a Terminal and a Shell (assume that the Terminal I am using is gnome-terminal
, and the Shell is bash
):
Now what I want to know is what mechanism does the Terminal and the Shell use to exchange data. This is what I think happens:
- When
gnome-terminal
is executed, it will create a file representing a serial port in the/dev/pts
directory (let's say the file name is/dev/pts/0
). gnome-terminal
will then execute the Shell associated with it (for example:bash
), and will pass it the pts file name (the pts file name can be passed through the command line arguments for example).- Now both
gnome-terminal
andbash
would start reading from/dev/pts/0
. - When
gnome-terminal
wants to send data tobash
, it would write this data to/dev/pts/0
, andbash
would read this data from/dev/pts/0
. - When
bash
wants to send data tognome-terminal
, it would write this data to/dev/pts/0
,and
gnome-terminal
would read this data from/dev/pts/0
.
This diagram shows what I just explained:
Am I correct in my understanding?
Note: of course the pts file could be a tty file if we were using virtual terminals (i.e. when we are not using a GUI), but the logic would still be the same.