A short history of the terminal
The word tty is short for teletypewriter. Early Unix machines were reached through teletypes, which were keyboards and printers connected over serial lines. The terminal was a separate box at the end of a cable, and the computer only saw a stream of characters arriving on a device file. Hardware terminals such as the VT100 added escape sequences, special byte patterns that move the cursor or change colors, so a program could drive the screen.
Linux kept the same model in several forms. Serial lines appear as devices such as /dev/ttyS0. The virtual consoles that you reach with Ctrl-Alt-F1 through F6 are ttys implemented by the kernel. A terminal window on a desktop is one more variation, and it is the one most people use every day. The software layers are the same in all of them, which is why one set of rules explains them all.
What a terminal emulator is
A terminal emulator is a graphical program that imitates a hardware terminal. It draws a grid of characters, sends the bytes for your key presses to a device, and interprets the bytes it receives. When a program prints a color code or moves the cursor, the emulator changes its display model. The emulator is not the shell and it is not the kernel. It is the piece that stands in for the physical terminal.
Because the emulator is only a front end, you can swap one for another without changing the shell or your commands. tmux is a terminal emulator too, from the point of view of the programs inside it: it holds the master side of a pseudo-terminal for each pane and draws the panes itself.
The pseudo-terminal pair
A pseudo-terminal, or pty, is a pair of linked devices described in pty(7). The master side is held by the emulator, or by ssh or tmux. The slave side looks like a real terminal to any program that opens it. Whatever is written to the master comes out as input on the slave, and whatever a program writes to the slave comes out of the master. The emulator opens the master through /dev/ptmx, and the kernel creates a matching slave whose name appears under /dev/pts/.
Bash and the programs it starts use the slave for their standard input, output, and error. You can check this directly. The first command prints the terminal device of the current shell. The second lists the open file descriptors of the shell process.
tty
ls -l /proc/$$/fdIn the lab, tty prints /dev/pts/0, and the descriptor listing shows descriptors 0, 1, and 2 all pointing at that device. You will also see descriptor 255. Bash keeps a duplicate of the terminal there for its own use, so that it can still reach the terminal when it changes the standard descriptors for a command.
The line discipline
Between the master and the slave, the kernel runs a component called the line discipline. Every tty has one, and it is the reason terminals feel like they have built-in editing. A program chooses its mode with the termios interface, described in termios(3), and the most common mode is canonical, also called cooked mode.
In canonical mode, input is delivered one line at a time. Until you press Enter, the kernel keeps the characters in a buffer, lets you erase the last one with the erase character (usually Backspace, which is ^?), and lets you discard the whole line with the kill character (^U). The echo flag copies each typed character back to the screen, so you can see what you type. This is why a program that reads one line gets the line already edited.
In raw mode, the line discipline delivers each byte as soon as it arrives. There is no editing and usually no echo. Full-screen programs such as vim and less switch to this kind of mode. They need to read single keys, such as the j key for moving down, and draw the screen themselves. When they exit, they restore the previous settings, which is why your shell prompt looks normal again afterward.
Control characters become signals
In canonical mode with signal generation turned on, three control characters are special. Ctrl-C sends SIGINT to the foreground process group, Ctrl-Z sends SIGTSTP, which usually suspends the job, and Ctrl-\ sends SIGQUIT. The kernel does this work, not the program, so it still works even if the program is busy and ignores its input.
Ctrl-D is different. It is not a signal at all. In canonical mode, the end-of-file character at the start of a line makes the next read() return zero bytes, and a read of zero bytes means end of file. That is why cat with no arguments exits when you press Ctrl-D on an empty line. If you type some text first and then press Ctrl-D, the kernel passes the partial line on without a newline, and you must press Ctrl-D again to reach end of file.
Raw mode turns off signal generation, so Ctrl-C arrives at the program as the byte 0x03. Vim receives it as an ordinary key press and decides for itself what to do, which is why Ctrl-C in vim cancels a pending command instead of killing the editor.
Inspecting the settings with stty
The stty utility reads and changes the settings of the terminal it runs on. With -a it prints all of them. The control characters appear as pairs such as intr = ^C, and flags appear by name, with a leading minus when they are off.
stty -aIn the lab, the output includes intr = ^C, quit = ^\, erase = ^?, kill = ^U, eof = ^D, and susp = ^Z. These are the same keys described above. The output is long, but the names are the ones to look for when you wonder what a key does.
TERM and terminfo
The emulator is one kind of terminal, and programs need to know which one. The TERM environment variable names the terminal type. In the lab it is xterm-256color. Programs look up that name in the terminfo database, which lists the escape sequences the terminal understands for clearing the screen, moving the cursor, and setting colors. If TERM names a terminal that the emulator does not match, full-screen programs draw garbage or refuse to start. You rarely need to change this, but it is the first thing to check when screen output looks wrong.
Hands-on: watch the cooked and raw modes
Run cat with no arguments. It reads from the terminal and copies each line to the output. Type a line and press Enter, then press Ctrl-D on an empty line. The program exits because the read returns zero bytes.
catBefore you run it again, notice that you can edit the line with Backspace before you press Enter. That editing is done by the line discipline, not by cat. Then run stty -a again and find the icanon and echo flags. Both are on in the normal shell, which is why the editing and echo you just saw work.