Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
tty [-s]

Try it

Bash tty without a terminal

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.

Bash The shell test for a terminal

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

Terminal
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. tty looks at file descriptor 0, so tty < file or echo | tty always 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 as tty -s without 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 -p prompts and sudo password 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 stdout is a terminal (ls --color=auto, grep --color=auto). That is why colors vanish in pipes.
  • See who is logged in where: who, w and ps -o tty show terminal names of other sessions. stty changes the settings of the terminal that tty names.
  • Containers: docker run -t allocates a pseudo-terminal; without it tty says "not a tty" even if you started the container from a shell.