Up until recently I was under the impression that Unix-y programs were unaware of where their output went, be it to standard out, redirected to a file, or into a pipe. However, an unrelated U&L question on this site brought to my attention the following example:
% echo "hello world" > file
% grep --color=auto lo file
hello world
% grep --color=auto lo file | cat
hello world
Obviously you can't see it here, but the first command shows 'lo' highlighted, as expected. However, the second call, which goes through a pipe, shows no colour. This suggests that grep
was aware of its output being directed to a pipe and avoided outputting colour. How is this done?
ls
andgrep
will also treat whitespace differently if they think they're talking to a 2-dimensional console rather than a 1-dimensional file stream. – Russell Borogove Mar 23 '12 at 17:42