Monday, October 7, 2024
HomeHow toLinux tty Command Tutorial for Beginners (with Examples)

Linux tty Command Tutorial for Beginners (with Examples)

The Linux tty command stands for “teletypewriter” and is used to display the file name of the terminal connected to the standard input. Essentially, it tells users which terminal they are using by returning a string that indicates the terminal device, such as /dev/tty1 or /dev/pts/2. This command is particularly useful in scripting and managing terminal sessions, as it helps in identifying and verifying the active terminal, ensuring that commands and outputs are directed appropriately. Its simplicity and specific functionality make tty a handy tool for both system administrators and users working in multi-terminal environments. 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 24.04 LTS.

Linux tty command

The tty command prints the file name of the terminal that is currently connected to standard input. Following is its syntax:

tty [OPTION]...

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?

Tty is short for teletype, but it’s more popularly known as terminal. It’s a device (implemented in software nowadays) that allows you to interact with the system by passing on the data (your input) to the system and displaying the output produced by the system.

ttys can be of different types. For example, you can access graphical consoles 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 does the tty command work?

Usage is pretty straightforward – just run ‘tty’, and the output will contain the name of the terminal connected to the standard output (or, in other words, the name of the current terminal).

tty

For example, in my system, the following output was produced:

/dev/pts/19

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?

A command line option -s exists that silences the tty command in that no output is produced. Just the exit status is returned.

tty -s

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

Conclusion

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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Most Popular