Linux Command Reference ยท System Information
Linux tty command
Reference for the Linux tty command: print the file name of the terminal connected to standard input, test for a terminal with -s, and detect interactive scripts.
tty prints the file name of the terminal device connected to its standard input, for example /dev/pts/3 for a terminal window or SSH session, or /dev/tty1 for a text console. If standard input is not a terminal (it is a pipe, a file or /dev/null), it prints not a tty and exits with status 1.
The exit status is the useful part: it tells a script whether a person is sitting at a terminal. Programs use that to decide whether to prompt, to show colors and progress bars, or to run quietly.
tty [-s]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
tty [-s | --silent | --quiet]
| Option | Description |
|---|---|
| (none) | Print the terminal file name or not a tty. |
-s, --silent, --quiet |
Print nothing; only set the exit status. |
--help, --version |
Usual information options. |
Returns: 0 if standard input is a terminal; 1 if it is not; 2 for a usage error; 3 if a write error occurred.
Usage notes
- Only standard input counts.
ttylooks at file descriptor 0, sotty < fileorecho | ttyalways says "not a tty" even in an interactive session. To test standard output use the shell test[ -t 1 ]. - Shell alternative: in Bash and POSIX shells
[ -t 0 ]does the same astty -swithout starting a process. Prefer it in scripts. - Typical names:
/dev/pts/N(pseudo-terminals used by terminal emulators, SSH, tmux,screen),/dev/ttyN(virtual consoles),/dev/ttyS0(serial lines). - Write to a user's terminal: the controlling terminal can also be reached through the special file
/dev/tty, even when stdin and stdout are redirected.read -pprompts andsudopassword prompts use it. - cron, systemd and CI run jobs without a terminal. Anything that asks questions must check
[ -t 0 ]and fall back to defaults or fail with a clear message. - Colors: many tools enable color only when
stdoutis a terminal (ls --color=auto,grep --color=auto). That is why colors vanish in pipes. - See who is logged in where:
who,wandps -o ttyshow terminal names of other sessions.sttychanges the settings of the terminal thatttynames. - Containers:
docker run -tallocates a pseudo-terminal; without itttysays "not a tty" even if you started the container from a shell.