Search code examples
bashttycommand-substitution

Command Substitution in Bash for tty


Command:

echo "a" | tee `tty`

Output:

a

Command:

echo "a" | tee /dev/pts/0

Output:

a
a

File connected with current terminal is /dev/pts/0. Shouldn't both commands produce same output? What am I missing here?


Solution

  • tee duplicates stdin to all file descriptors given on the command line and to stdout, so if one of the files it the current terminal (you can also use -) then input will be written twice on stdout.

    In the first case, stdin is not connected to a tty (but to the output of the echo command), so tty gives not a tty. So the command becomes echo "a" | tee not a tty, and you will have three files (called "not" "a" and "tty") each containing "a", as well as "a" written to stdout.