Linux tty Command Tutorial for Beginners (with Examples)

In Linux, everything is a file. Even any hardware device connected to the system is represented as a special file. So it shouldn't come as a surprise that a terminal is also represented as a file. There exists a command dubbed tty that displays terminal related information. In this tutorial, we will discuss the basics of tty using some easy to understand examples.

But before we do that, it's worth sharing that all examples in this article have been tested on Ubuntu 16.04 LTS.

Linux tty command

The tty command basically prints the file name of the terminal 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?

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).

tty

For example, on 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?

There exists a command line option -s 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.

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

Suggested articles

0 Comment(s)

Add comment