kensuke1984 kensuke1984 - 2 months ago 6x
Bash Question

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).


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


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.