Preserve colouring after piping grep to grep

后端 未结 4 1233
遇见更好的自我
遇见更好的自我 2021-01-30 05:07

There is a simlar question in Preserve ls colouring after grep’ing but it annoys me that if you pipe colored grep output into another grep that the coloring is not preserved.

4条回答
  •  日久生厌
    2021-01-30 05:19

    The existing answers only address the case when the FIRST command is grep (as asked by the OP, but this problem arises in other situations too).

    More general answer

    The basic problem is that the command BEFORE | grep, tries to be "smart" by disabling color when it realizes the output is going to a pipe. This is usually what you want so that ANSI escape codes don't interfere with your downstream program.

    But if you want colorized output emanating from earlier commands, you need to force color codes to be produced regardless of the output sink. The forcing mechanism is program-specific.

    Git: use -c color.status=always

    git -c color.status=always status | grep -v .DS_Store
    

    Note: the -c option must come BEFORE the subcommand status.

    Others

    (this is a community wiki post so feel free to add yours)

提交回复
热议问题