1. From man tty

    tty - print the file name of the terminal connected to standard input

    $ tty
  2. From APUE:

    Historically, the name of the controlling terminal in most versions of the UNIX System has been /dev/tty. POSIX.1 provides a runtime function that we can call to determine the name of the controlling terminal.

    #include <stdio.h>
    char *ctermid(char *ptr);
    $ ls -la /dev/tty
    crw-rw-rw- 1 root tty 5, 0 May 26 00:16 /dev/tty

So I was wondering if the output of command tty and the file /dev/tty both refer to the controlling terminal of the current bash process?

If yes, why does command tty output a pseudoterminal slave file /dev/pts/2, which is different from /dev/tty?

Since the terminal emulator lxterminal and the bash shell running in it use a pseudoterminal pair, isn't the pseudoterminal slave /dev/pts/2 the controlling terminal of the bash shell?

/dev/tty and /dev/pts/2 aren't symlinks to one another, so are they different files?


A follow up post what relations are between my current controlling terminal and `/dev/tty`?

2 Answers 2


The command tty (1) returns the name of the terminal connected to standard input. This may be the controlling terminal or it may not be. The process may even have a controlling terminal but it is not written in stone that that terminal must be connected to standard input.

From the POSIX manual page for tty (1):

The tty utility shall write to the standard output the name of the terminal that is open as standard input. The name that is used shall be equivalent to the string that would be returned by the ttyname() function defined in the System Interfaces volume of POSIX.1‐2008.

Try it for yourself; run

< /dev/null sh -c 'tty; sleep 10; echo "Done."'

and press Ctrl+C after seeing the message "not a tty" written out by tty because standard input it not a (pseudo-)terminal.

On the other hand, /dev/tty is always a synonim for the controlling terminal of a process, provided that the process actually has a controlling terminal.

  • Thanks. Appreciate again if you could consider unix.stackexchange.com/questions/447197/…
    – Tim
    May 31, 2018 at 20:56
  • @Tim: If a process has a controlling terminal, /dev/tty is guaranteed to be a synonym of the controlling terminal. That is, /dev/tty and the proper name of the controlling terminal will always refer to the same physical or virtual device.
    – AlexP
    May 31, 2018 at 20:59
  • Thanks. For different sessions with different controlling terminals, /dev/tty is guaranteed to be their controlling terminals. How can a file be different controlling terminals, without being a symlink or hardlink?
    – Tim
    May 31, 2018 at 21:13
  • @Tim: I don't see the difficulty. Terminal I/O is handled by the kernel, and the kernel knows perfectly well what process called it and which is the controlling terminal of that process.
    – AlexP
    May 31, 2018 at 21:21
  • Thanks. If a process doesn't have a controlling terminal, what does /dev/ttymean for the process?
    – Tim
    Dec 1, 2018 at 2:04

Do the output of command tty and the file /dev/tty both refer to the controlling terminal of the current process?

Well, no, they're not the same. tty outputs the name of the tty connected to it's standard input. That's not necessarily the same as the controlling terminal (which what /dev/tty refers to).

$ tty
$ tty < /dev/pts/1
$ tty < /dev/null
not a tty

Of course, usually if you have stdin connected to a terminal, it's the controlling terminal.

The use case for tty that I've seen, is to determine if a script is running interactively or not. For that use, checking stdin instead of the controlling terminal allows to detect situations where the stdin is connected to something other than a terminal, and where the user can't provide interactive input. E.g. running maybe_interactive.sh < some_input_file from an interactive shell.

/dev/tty and /dev/pts/2 aren't symlinks to one another, so are they different files?

/dev/tty can't be a symlink since the controlling terminal is different for different processes, and symlinks are the same for every process.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .