Sample Header Ad - 728x90

How does `/dev/tty` work as a "synonym"?

0 votes
0 answers
47 views
### Context To restore the standard input, standard output and/or standard error (file descriptors 0, 1 and/or 2 respectively) of a process to the keyboard and/or display of an interactive shell (bash) session, I have seen here that one can execute either of the following: $ exec &>/dev/tty or $ exec &>$(tty) Whereas $(tty) (a command substitution) returns /dev/pts/X where X is the number associated to the currently active terminal window (pseudo-tty), thereby explicitly pointing the redirection to the "real" filename associated with the terminal in which the command is entered, in contrast /dev/tty is a character file which *seems* to appear identical to different processes (i.e. to different concurrent terminal windows). In man 4 tty it says: > The file /dev/tty [...] is a synonym for the controlling terminal of a process... In The Open Group Base Specifications Issue 8, Chapter 10 it says: > **/dev/tty** > > In each process, a synonym for the controlling terminal associated with the process group of that process, if any. It is useful for programs or shell procedures that wish to be sure of writing messages to or reading data from the terminal no matter how output has been redirected. It can also be used for applications that demand the name of a file for output, when typed output is desired and it is tiresome to find out what terminal is currently in use. ### Question Since /dev/tty is a file and not a symbolic link like /dev/fd or /proc/self, how can it "point" to different terminal processes when interpreted in different terminal windows? What does it mean in Unix/Linux parlance to be "a synonym" when not a symbolic link or a hard link — I mean: what is interpreting such a "synonym" when invoked on a command line and how?
Asked by The Quark (402 rep)
Jul 24, 2025, 01:16 PM
Last activity: Jul 25, 2025, 05:58 AM