How does commands like ls
know what its stdout is?
It seems ls
is operating different depending on what the target stdout is. For example if I do:
ls /home/matt/tmp
the result is:
a.txt b.txt c.txt
However if I do
ls /home/matt/tmp | cat
the result is (i.e. new line per result):
a.txt
b.txt
c.txt
The process is passed a file descriptor 1 for stdout right? How does it determine how to format the result? Does the file descriptor reveal information?