What effect does auto as an option for the --color switch have in grep? When does grep decide to color the the matching strings, and when doesn't it?

1 Answer 1


Expected behavior

With --color=auto, grep will highlight matching strings if (and only if) the output is written directly to the terminal and said terminal is capable of displaying colored output.

Normally, --color=auto is what you want. If, e.g., you use grep to match a URL and pipe it to Wget, Wget will see \e[1;31mhttp://... instead of the actual URL (and choke on it).

The following commands should result in colored output:

echo Super User | grep --color=auto Super
echo Super User | grep --color=always Super | cat

This command, however, should not:

echo Super User | grep --color=auto Super | cat

Any inconsistency with this behavior should be considered a bug.

Source code

With --color=auto, the latest Grep for Windows version (2.5.4) – as well as the original 2.5.4 it is based on – color the output if and only if the condition

isatty(STDOUT_FILENO) && getenv("TERM") && strcmp(getenv("TERM"), "dumb")

is true, i.e., if and only if the output is being written to a terminal, the environment variable TERM is defined and the terminal is not dumb.

This won't produce the desired behavior under Windows, since TERM is normally not defined. An easy solution to this problem is setting the TERM=windows in the control panel.

The latest version of grep (2.14) fixes this issue by coloring the output if and only if the condition

isatty(STDOUT_FILENO) && should_colorize()

is true, where should_colorize() is defined differently for POSIX and Win32:

For the former, the condition is equivalent to the one of 2.5.4; for the latter, the enviroment variable TERM doesn't have to be set (it just can't be dumb).

  • How does it find out if it writes to the terminal or not?
    – trolzen
    Nov 2, 2012 at 11:29
  • 1
    There's a C function for that. If you include the header file sdgstd.h, istty(stdout) returns 0 if the output is redirected to a file.
    – Dennis
    Nov 2, 2012 at 11:53
  • I experiment with windows ports of grep and less from GnuWin project (gnuwin32.sourceforge.net). cmd.exe is improved with ansicon utility. less --color=auto works as expected: uses color when no redirection is applied and does not when output is redirected. But grep --color=auto doesn't print escape sequences in both cases.
    – trolzen
    Nov 2, 2012 at 15:13
  • I cannot understand why this happens. I thought these utilities have the same code both in unix and windows versions and they are quite old. So they should behave identically.
    – trolzen
    Nov 2, 2012 at 15:19
  • That's a bug in GnuWin32's grep then. On Ubuntu, it behaves as I detailed in my answer. While both derive from the same source code, there are always minor adjustments that have to be made if you're going to compile on another platform. Also, keep in mind that GnuWin32's current version of grep is 2.5.4. The latest version on Ubuntu is 2.12.
    – Dennis
    Nov 2, 2012 at 15:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .