Linux tty Command Tutorial for Beginners (with Examples)
In Linux, everything is a file. Even every hardware device connected to the system is represented as a special file. So it should come as no surprise that a terminal is also represented as a file. There is a command called tty that displays terminal-related information. In this tutorial, we will explain the basics of tty with some easy-to-understand examples.
But before we do that, we should point out that all the examples in this article have been tested on Ubuntu 22.04 LTS.
Linux tty command
The tty command basically prints the file name of the terminal that is currently connected to standard input. Following is its syntax:
And here's how the tool's man page explains it:
Print the file name of the terminal connected to standard input.
Following are some Q&A-styled examples that should give you a good idea on how tty works.
Q1. What is tty?
In essence, tty is short for teletype, but it's more popularly known as terminal. It's basically a device (implemented in software nowadays) that allows you to interact with the system by passing on the data (you input) to the system, and displaying the output produced by the system.
ttys can be of different types. For example, graphical consoles that you can access with the Ctrl+Alt+Fn key combination, or terminal emulators like Gnome terminal that run inside an X session. To learn more about tty, head here.
Q2. How tty command works?
Usage is pretty straight forward - just run 'tty', and the output will contain name of the terminal connected to standard output (or, in other words, name of the current terminal).
For example, on my system, the following output was produced:
Note that the command produces a message like "not a tty" if it is not running inside a terminal.
Q3. How to make tty only return exit status?
There exists a command line option -s that silences the tty command in that no output is produced. Just the exit status is returned.
Following is the meaning of each exit status:
- 0 if standard input is a terminal
- 1 if standard input is not a terminal
- 2 if given incorrect arguments
- 3 if a write error occurs
Needless to say, tty is not the kind of command that you may have to use frequently. But it's always good to know such a command exists, and given the limited set of command line options, it's easy to understand as well. For more info about the tool, head to its man page.