kensuke1984
kensuke1984

Reputation: 995

Color information disappears when writing a result of 'grep --color=auto' into a file

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

Answers (2)

Krzysztof Krasoń
Krzysztof Krasoń

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

choroba
choroba

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

Related Questions