[TriLUG] What happens to console codes in pipes?

Nathan Yinger via TriLUG trilug at trilug.org
Wed Sep 19 12:21:46 EDT 2018


Interesting. I've never used colors in the command line tools I've written
so I never thought of it, but I guess any time you use colors in a command
line tool you'd need to check that.

On Wed, Sep 19, 2018 at 10:29 AM, Aaron Schrab <aaron at schrab.com> wrote:

> 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.
>


More information about the TriLUG mailing list