tty
Print the name of the terminal
TLDR
Print the file name of this terminal
SYNOPSIS
tty [OPTION]...
tty [-s | --silent]
PARAMETERS
-s, --silent
Do not print the name of the terminal. Only the exit status is important for scripting purposes.
--help
Display a help message and exit.
--version
Output version information and exit.
DESCRIPTION
The tty command is a fundamental Unix and Linux utility that prints the file name of the terminal or pseudo-terminal connected to standard input. In essence, it answers the question, "What terminal am I currently using?" This simple command is incredibly useful, especially within shell scripts, to determine if a script is running interactively on a terminal or as part of a background process, a pipeline, or a redirection.
When standard input is connected to a terminal device, tty will output the path to that device, for example, /dev/pts/0 or /dev/tty1. If standard input is not connected to a terminal (e.g., if input is redirected from a file or a pipe), tty will output "not a tty" or similar (depending on implementation or specific setup), and crucially, will exit with a non-zero status. Its primary utility lies in its exit status, which allows scripts to make decisions based on whether an interactive session is present.
CAVEATS
tty determines the terminal based on its standard input. If standard input is redirected from a file or a pipe, tty will indicate that it is not connected to a terminal, even if the shell running the command itself is interactive.
The exact string output when not connected to a TTY (e.g., "not a tty") might vary slightly across different Unix-like systems, though the non-zero exit status is consistent and more reliable for scripting.
EXIT STATUS
The exit status of the tty command is highly significant, especially in scripting:
0: If standard input is a terminal device.
1: If standard input is not a terminal device.
2: If an invalid option is specified.
This allows shell scripts to conditionally execute commands or adjust behavior based on whether they are running in an interactive terminal session or not.
HISTORY
The tty command is a very old and fundamental utility in Unix and Linux systems, tracing its roots back to early versions of Unix. It serves a basic yet essential function of identifying the terminal device. Its behavior and interface have remained remarkably consistent over decades, reflecting the stable nature of the underlying terminal device abstraction. It is part of the GNU Core Utilities (coreutils) package on Linux, ensuring its ubiquity and standardized behavior across distributions. Its simplicity and utility have ensured its continued presence as a core command-line tool.