grep --color: Highlight Matches in CI Logs
grep --color wraps each match in ANSI color codes so it stands out in the output.
Highlighting helps a human scan a log, but the same ANSI codes corrupt machine-parsed output. Knowing the three color modes keeps both happy.
What it does
grep --color[=WHEN] surrounds matched text with ANSI escape sequences. WHEN is auto (color only when output is a terminal), always (always emit codes, even through a pipe or to a file), or never. The default in many distros is auto via a GREP_COLORS-aware alias.
Common usage
# highlight only when viewed in a terminal
grep --color=auto ERROR build.log
# force color through a pipe (e.g. to less -R)
grep --color=always ERROR build.log | less -R
# strip color for clean parsing
grep --color=never ERROR build.logOptions
| Mode | What it does |
|---|---|
| --color=auto | Color only when stdout is a terminal |
| --color=always | Always emit ANSI codes, even in pipes |
| --color=never | Never emit color codes |
| GREP_COLORS | Env var to customize the highlight colors |
In CI
CI logs are not terminals, so --color=auto produces no codes there, which is usually what you want. Use --color=always only when piping into a viewer that renders ANSI. Never feed --color=always output into a step that parses the text, or the escape codes will break the parse.
Common errors in CI
Garbled output or a downstream parser failing on \x1b[01;31m sequences means a grep --color=always (or a shell alias defaulting to color) leaked ANSI codes into captured output; switch to --color=never for machine-read data. If you expected highlighting in the CI log and saw none, that is auto correctly detecting a non-terminal; use --color=always if you truly want the codes.