In all the cases, the shell sees a TTY or PTY device and a TERM environment variable which will identify the correct set of terminal control codes to use.
When using GNOME's terminal window, the device is a UNIX 98-style PTY: /dev/pts/*
. When the terminal window is active and receiving X11/Wayland keyboard events, the GNOME Terminal process translates them into UTF-8 characters and feeds them into the "master" side of the PTY device, so the CLI process gets them from the other (classically known as "slave") side of the PTY device.
Output is handled the same way: the CLI process writes UTF-8 characters and terminal control codes (the correct code set identified by the TERM environment variable) to the PTY device, and the GNOME Terminal process reads them from its side of the PTY device, uses them as input for a simulated terminal, and renders the simulated view as X11/Wayland graphics.
When on a Linux virtual console device (/dev/tty[1..n]
), the process is much the same, except the terminal emulation runs within the kernel, and so there is no equivalent of a "master" PTY device visible to the user-space.
If you are operating on a serial console, the CLI process gets a "real" TTY device: a serial port (/dev/ttyS[0..n]
). Whatever is at the other end of the serial cable will be responsible of the rest: in these days, it might be another terminal emulator, like a laptop running Linux minicom
or a reasonably new version of PuTTY that includes serial port support. Or it might be a real terminal: maybe an actual Digital VT-series terminal, or a HP 700/96.
Or if you are seriously into retrocomputing, it might be an electromechanical monstrosity from the 1930's.
scanf()
. That's a standard function in the C library, implemented within the application program, in user space. Are you asking about how user space programs interface with a real terminal vs. a GUI terminal emulator vs. something else, e.g. a pseudo-terminal set up by the SSH server? Because for most part, there is no difference for the userspace program between those cases, that pretty much being the point of a standard-ish terminal interface. – ilkkachu Oct 31 '23 at 10:22