Friday, May 22, 2015

Be cautious of using grep --colour=always

I noticed that grep --colour=always can embed additional code (for coloring, for example, ^[[01;31m^[[K    ^[[m^[[K) in your text, which could lead downstream confusion. Here is one such example:
https://stackoverflow.com/questions/18315380/invisible-0131-number-in-bash

$ echo 1 2 3 | grep 2
1 2 3
$ echo 1 2 3 | grep 2 | cat -v
1 2 3
$ echo 1 2 3 | grep --colour=always 2 
2 3
$ echo 1 2 3 | grep --colour=always 2 | cat -v
1 ^[[01;31m^[[K2^[[m^[[K 3

See my answer for more detail.

The solution is to use --colour=autoor not use the --colour option at all.


$ echo 1 2 3 | grep --colour=auto 2 
2 3
$ echo 1 2 3 | grep --colour=auto 2 | cat -v
1 2 3

Check your .bashrc to see if you set alias grep='grep --colour=always' like I did.

No comments:

Post a Comment