Subject: Re: bug#55641: Using colours with grep

From: Gary Johnson <[email-masked]>
Date: Thu, 26 May 2022 20:27:27 -0700

On 2022-05-26, Paul Eggert wrote:
> On 5/25/22 13:48, goncholden via Bug reports for GNU grep wrote:
> >Have been using grep to colour the filename printed by tail using
> >grep, by setting GREP_COLOR.  However the setting does not work
> >with GREP_COLORS.
> >
> >Additionally, I could not set colour values with tput commands.
> >
> >tail -v -n "$tm" $file | GREP_COLOR='01;32' $_GREP -e ^ -e '^==> .* <==$'
> 
> Sorry, I'm not following. Can you give a test case that I can
> reproduce here?

I think the problem is that the OP has used GREP_COLORS to specify
the colors that grep uses, and has then tried to override the
default colors by specifying GREP_COLOR on the command line.  But,
as the man page says, in the GREP_COLOR section,

    The mt, ms, and mc capabilities of GREP_COLORS have priority
    over it.

So the GREP_COLOR setting has no effect when GREP_COLORS is also
used, at least when GREP_COLORS contains the ms capability.

There are a number of solutions to this, depending on what the OP is
trying to achieve with the other values in GREP_COLORS.  One is to
clear GREP_COLORS in their command line, e.g.,

    tail -v -n "$tm" $file | GREP_COLORS= GREP_COLOR='01;32' $_GREP -e ^ -e '^==> .* <==$'

Another would be to set the ms capability in GREP_COLORS instead of
using GREP_COLOR, e.g.,

    tail -v -n "$tm" $file | GREP_COLORS='ms=01;32' $_GREP -e ^ -e '^==> .* <==$'

For a demonstration of the problem, try the following.

    $ man grep | grep prevents

The result is a single line of the man page with "prevents"
highlighted in the default red.

    $ man grep | GREP_COLOR='01;32' grep prevents

This result is the same except that "prevents" is in green.

    $ export GREP_COLORS='ms=01:33'
    $ man grep | grep prevents
    $ man grep | GREP_COLOR='01;32' grep prevents

Now, both results have "prevents" in yellow, which agrees with the
man page.  However, the OP expected the GREP_COLOR setting on the
command line to override the GREP_COLORS setting in the environment,
which it doesn't.

Caveats:

- I am assuming that $_GREP is grep.
- I don't understand the tput problem.
- I don't know anything about grep colors other than what I just
  read in the man page.  The question just intrigued me.

Regards,
Gary