Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 566686 - sys-apps/grep false positive - bug related to color output
Summary: sys-apps/grep false positive - bug related to color output
Status: RESOLVED INVALID
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Core system (show other bugs)
Hardware: All Linux
: Normal normal
Assignee: Gentoo's Team for Core System packages
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-11-24 01:29 UTC by Thomas Capricelli
Modified: 2015-11-24 15:35 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Capricelli 2015-11-24 01:29:35 UTC
There's a weird bug with grep where it finds (and displays) stuff that doesn't exist. It only happens by adding a 'm' at the beginning. The 'm' dissappears with removing grep colors (by piping in cat for example), but the false positive is still there

100% reproducible example, starting from an empty directory:

orzel@berlioz orzel/tmp/grepbug% touch abc.txt
orzel@berlioz orzel/tmp/grepbug% ls | grep mab
mabc.txt   # 'mab' is in red
orzel@berlioz orzel/tmp/grepbug% ls | grep mab | cat
abc.txt

It works with standard input (as previous exemple) and also from inside a file.
I can reproduce it with both sys-apps/grep-2.21-r1 and sys-apps/grep-2.22

Reproducible: Always
Comment 1 Michael Orlitzky gentoo-dev 2015-11-24 02:43:21 UTC
The 'm' is really there. Your terminal hides certain character sequences from you. In particular, it hides the color escape sequences. Try this, for example:

  $ echo -e "\e[31mRed"

So what you're seeing is that `ls` is outputting not just the name of the file, but a color code followed by the name of the file. And as you can see above, that color escape code happens to end with an 'm'.

If you want to reliably grep the output of ls, use `/bin/ls` or $(which ls) so that you don't get colored output from some alias.
Comment 2 Thomas Capricelli 2015-11-24 03:15:24 UTC
Indeed this is fixed by using \ls instead of ls which is an alias to 'ls --color'.

As shown by the 'grep | cat' example i've given, grep is smart enough to NOT output colors when stdout is not a tty, but ls doesn't have this smartness.. right ? Too bad.

I guess we should close the bug as CANTFIX then ?
Comment 3 SpanKY gentoo-dev 2015-11-24 13:32:43 UTC
ls is behaving correctly.  `ls --color` is the same as `ls --color=always`, and that (by design) does not check if stdout is a tty.

Gentoo's default already does the correct thing:
$ grep ls= /etc/bash/bashrc
        alias ls='ls --color=auto'
Comment 4 Thomas Capricelli 2015-11-24 15:35:22 UTC
yeps, indeed. ls --color was an alias from my ~/.cshrc. Sorry for the noise.