Skip to content

Unix TTY

Unix TTY

The TTY in Unix is a software emulated “teletype” terminal program which is used for input/output with the operating system. TTY instances (and terminal emulators) expose standard input and output streams to input and output devices in order to support communication with the system kernel.

The terminal runtime (whether a TTY instance or a terminal emulator) is handled by the TTY driver, which is the system driver responsible for handling input and output for programs in the system. The TTY driver interface is represented by a file, which is automatically created by the operating system when a new terminal instance is initiated. This means that the TTY interface has the standard streams–stdin, stdout, and stderr–and that these streams can be used by the terminal emulator to write input to the TTY driver and, by extension, to programs on the machine, which can then use the standard streams on the TTY file to write output to the TTY instance in the terminal emulator.

Invoking the program tty prints the filepath for the file that was automatically created to handle the current TTY instance:

$ tty

Generally, this is how the data flows for a simple echo command:

1) *keyboard*: user types 'cat path/to/file'
  2) stdin to terminal emulator
    3) stdin to TTY
      4) stdin to `cat` program: path/to/file
5) *cat*: receives stdin, reads content of file at filepath and sends that content through `stdout`
  6) stdout to TTY
    7) stdout to terminal emulator
      8) stdout to monitor, displaying contents of file

Represented with stream redirects, this flow might look something like:

1) *user types and enters 'cat path/to/file' into terminal emulator via keyboard*
2) (terminal emulator) < (keyboard driver)
3) /dev/ttys001 < (terminal emulator)
4) /bin/cat < /dev/ttys001
5) *cat program runs*
6) /bin/cat > /dev/ttys001
7) /dev/ttys001 > (terminal emulator)
8) (terminal emulator) > (graphics driver)

So, the “data journey” can be thought of as a “there and back again” route from the input device, through the TTY, to the program, and then out, through the TTY, to the output device.

Further reading