The key statements in the snippet below (i.e. other than those for printing labels and blank lines, for spacing) come in pairs. In each pair, the first and second statements have the forms
echo $(tty...), respectively.
echo stdin: tty echo $(tty) echo echo stdout: tty <&1 echo $(tty <&1) echo echo stderr: tty <&2 echo $(tty <&2)
If I source a file containing this snippet (in a
bash session, for example), I get the following output1 (I added the line numbers afterwards, for reference):
1 stdin: 2 /dev/pts/8 3 /dev/pts/8 4 5 stdout: 6 /dev/pts/8 7 not a tty 8 9 stderr: 10 /dev/pts/8 11 /dev/pts/8
In this output, the line generated by
echo $(tty <&1) (line 7) sticks out conspicuously, for two reasons:
- it is the only one among the
echo $(tty ...)-generated lines (namely, lines 3, 7, and 11) that differs its
tty ...-generated counterpart, immediately preceding it (lines 2, 6, and 10); and
- it differs from the output (line 11) of the formally analogous
echo $(tty <&2).
Q: How are these two discrepancies explained?
For the record, I tried to find the explanation for these apparent anomalies in the manual page for
tty, but, as far as I can tell, this page does not address these questions at all2; certainly not explicitly3.
This question was motivated by some of the code this great answer to an earlier question of mine.
1Of course, the actual number after
/dev/pts/ will be different if I change terminals, and will likely be different for you if you try the same thing.
2In fact, the entire DESCRIPTION section of the
tty manual page available on my system consists of just one sentence: "Print the file name of the terminal connected to standard input.".
3Here I am leaving open the possibility that someone with more background knowledge than I have may be able to deduce from
tty's terse manual page the behavior illustrated above.