"tty" Command in Linux with Examples

The term tty is short for teletypewriter, the terminal device that UNIX was first run from. The tty command writes to the standard output the name of the terminal that is open as standard input. For example simply try the “tty” command without any options:

# tty
/dev/pts/0

To get the list of options the “tty” command has, run the –help option.

# tty --help
Usage: tty [OPTION]...
Print the file name of the terminal connected to standard input.

  -s, --silent, --quiet   print nothing, only return an exit status
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: [http://www.gnu.org/software/coreutils/]
For complete documentation, run: info coreutils 'tty invocation'

Examples

1. As stated above, running “tty” command without any option prints the file name of the terminal connected to standard input. For example:

# tty
/dev/pts/0

2. The “-s” or “–silent” option is useful only if you want the exit status and not the file name of the terminal. For example:

# tty -s
# echo $?
0

The possible exit status codes are:

0 Standard input is a terminal.
1 Standard input is not a terminal.
>1 An error occurred.

3. You can also use it to check if a file is a tty or not. For example:

# touch file01
# tty < file01
not a tty
# tty < /dev/pts/0
/dev/pts/0

If the file provided as input to tty command is an actual tty file, then the same file name is printed on the command line.

4. Use the “–version” option to view the tty command version.

# tty --version
tty (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http: gnu.org="" licenses="" gpl.html="">.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie.</http:>