Reputation: 995
When I do as below with an aliased grep(grep --color=auto)
echo abcde | grep 'ab'
it returns abcde (ab in red).
but
echo abcde | grep 'ab' >foo.txt
foo.txt has just abcde.
I guess my terminal shows ab in red in the 1st case according to some tags by 'grep' but foo.txt does not contain them. Is it due to grep?
Does grep judge what returning value should be?
My grep is grep (GNU grep) 2.20
Upvotes: 1
Views: 341
Reputation: 27476
grep recognizes where you store the result and disables coloring in case of auto
setting for redirections (colors is enabled only for terminal).
Use --color=always
to force it to use it ... always, but I don't think you will find those control sequences nice to view in a text file.
Upvotes: 5
Reputation: 241828
With --color=auto
, grep checks whether the output goes to a terminal, and switches colours on only when it does. You need to specify --color=always
instead.
Upvotes: 4