[TriLUG] What happens to console codes in pipes?

Aaron Schrab via TriLUG trilug at trilug.org
Wed Sep 19 10:29:06 EDT 2018


At 09:53 -0400 19 Sep 2018, Nathan Yinger wrote:
>While doing some grepping, I noticed something: grep returns colored
>output, but if it is piped through something like sort or sed the colors
>are stripped out.
>
>I am wondering what is stripping the color codes. Is this something the
>shell does to anything that is piped?

The color information isn't being stripped out. Rather `grep` is 
detecting that its output isn't going to a terminal and so it doesn't 
generate it in the first place. This is the effect of the `--color=auto` 
option (likely supplied by a shell alias or the deprecated 
`$GREP_OPTIONS` environment variable).  If you change that to (or add 
after) `--color=always` you'd see the escapes for coloring in piped 
output as well.  It also supports `--color=never`.

The shell doesn't have access to the piped data. It just creates the 
pipe an connects its ends to the processes on either side. After that 
the data goes directly from the process that writes the data to the one 
that reads it, with only the kernel being involved.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 898 bytes
Desc: not available
URL: <http://www.trilug.org/pipermail/trilug/attachments/20180919/facef844/attachment.pgp>


More information about the TriLUG mailing list