2.2 Environment Variables

The behavior of grep is affected by several environment variables, the most important of which control the locale, which specifies how grep interprets characters in its patterns and data.

The locale for category LC_foo is specified by examining the three environment variables LC_ALL, LC_foo, and LANG, in that order. The first of these variables that is set specifies the locale. For example, if LC_ALL is not set, but LC_COLLATE is set to ‘pt_BR.UTF-8’, then a Brazilian Portuguese locale is used for the LC_COLLATE category. As a special case for LC_MESSAGES only, the environment variable LANGUAGE can contain a colon-separated list of languages that overrides the three environment variables that ordinarily specify the LC_MESSAGES category. The ‘C’ locale is used if none of these environment variables are set, if the locale catalog is not installed, or if grep was not compiled with national language support (NLS). The shell command locale -a lists locales that are currently available.

The following environment variables affect the behavior of grep.

GREP_COLOR

This obsolescent variable interacts with GREP_COLORS confusingly, and grep warns if it is set and is not overridden by GREP_COLORS. Instead of ‘GREP_COLOR='color'’, you can use ‘GREP_COLORS='mt=color'’.

GREP_COLORS

This variable controls how the --color option highlights output. Its value is a colon-separated list of terminfo capabilities that defaults to ‘ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36’ with the ‘rv’ and ‘ne’ boolean capabilities omitted (i.e., false). The two-letter capability names refer to terminal “capabilities,” the ability of a terminal to highlight text, or change its color, and so on. These capabilities are stored in an online database and accessed by the terminfo library. Non-empty capability values control highlighting using Select Graphic Rendition (SGR) commands interpreted by the terminal or terminal emulator. (See the section in the documentation of your text terminal for permitted values and their meanings as character attributes.) These substring values are integers in decimal representation and can be concatenated with semicolons. grep takes care of assembling the result into a complete SGR sequence (‘\33[’...‘m’). Common values to concatenate include ‘1’ for bold, ‘4’ for underline, ‘5’ for blink, ‘7’ for inverse, ‘39’ for default foreground color, ‘30’ to ‘37’ for foreground colors, ‘90’ to ‘97’ for 16-color mode foreground colors, ‘38;5;0’ to ‘38;5;255’ for 88-color and 256-color modes foreground colors, ‘49’ for default background color, ‘40’ to ‘47’ for background colors, ‘100’ to ‘107’ for 16-color mode background colors, and ‘48;5;0’ to ‘48;5;255’ for 88-color and 256-color modes background colors.

Supported capabilities are as follows.

sl=

SGR substring for whole selected lines (i.e., matching lines when the -v command-line option is omitted, or non-matching lines when -v is specified). If however the boolean ‘rv’ capability and the -v command-line option are both specified, it applies to context matching lines instead. The default is empty (i.e., the terminal’s default color pair).

cx=

SGR substring for whole context lines (i.e., non-matching lines when the -v command-line option is omitted, or matching lines when -v is specified). If however the boolean ‘rv’ capability and the -v command-line option are both specified, it applies to selected non-matching lines instead. The default is empty (i.e., the terminal’s default color pair).

rv

Boolean value that reverses (swaps) the meanings of the ‘sl=’ and ‘cx=’ capabilities when the -v command-line option is specified. The default is false (i.e., the capability is omitted).

mt=01;31

SGR substring for matching non-empty text in any matching line (i.e., a selected line when the -v command-line option is omitted, or a context line when -v is specified). Setting this is equivalent to setting both ‘ms=’ and ‘mc=’ at once to the same value. The default is a bold red text foreground over the current line background.

ms=01;31

SGR substring for matching non-empty text in a selected line. (This is used only when the -v command-line option is omitted.) The effect of the ‘sl=’ (or ‘cx=’ if ‘rv’) capability remains active when this takes effect. The default is a bold red text foreground over the current line background.

mc=01;31

SGR substring for matching non-empty text in a context line. (This is used only when the -v command-line option is specified.) The effect of the ‘cx=’ (or ‘sl=’ if ‘rv’) capability remains active when this takes effect. The default is a bold red text foreground over the current line background.

fn=35

SGR substring for file names prefixing any content line. The default is a magenta text foreground over the terminal’s default background.

ln=32

SGR substring for line numbers prefixing any content line. The default is a green text foreground over the terminal’s default background.

bn=32

SGR substring for byte offsets prefixing any content line. The default is a green text foreground over the terminal’s default background.

se=36

SGR substring for separators that are inserted between selected line fields (‘:’), between context line fields (‘-’), and between groups of adjacent lines when nonzero context is specified (‘--’). The default is a cyan text foreground over the terminal’s default background.

ne

Boolean value that prevents clearing to the end of line using Erase in Line (EL) to Right (‘\33[K’) each time a colorized item ends. This is needed on terminals on which EL is not supported. It is otherwise useful on terminals for which the back_color_erase (bce) boolean terminfo capability does not apply, when the chosen highlight colors do not affect the background, or when EL is too slow or causes too much flicker. The default is false (i.e., the capability is omitted).

Note that boolean capabilities have no ‘=’... part. They are omitted (i.e., false) by default and become true when specified.

LC_ALL
LC_COLLATE
LANG

These variables specify the locale for the LC_COLLATE category, which might affect how range expressions like ‘a-z’ are interpreted.

LC_ALL
LC_CTYPE
LANG

These variables specify the locale for the LC_CTYPE category, which determines the type of characters, e.g., which characters are whitespace. This category also determines the character encoding. See Character Encoding.

LANGUAGE
LC_ALL
LC_MESSAGES
LANG

These variables specify the locale for the LC_MESSAGES category, which determines the language that grep uses for messages. The default ‘C’ locale uses American English messages.

POSIXLY_CORRECT

If set, grep behaves as POSIX requires; otherwise, grep behaves more like other GNU programs. POSIX requires that options that follow file names must be treated as file names; by default, such options are permuted to the front of the operand list and are treated as options.

TERM

This variable specifies the output terminal type, which can affect what the --color option does. See General Output Control.

The GREP_OPTIONS environment variable of grep 2.20 and earlier is no longer supported, as it caused problems when writing portable scripts. To make arbitrary changes to how grep works, you can use an alias or script instead. For example, if grep is in the directory ‘/usr/bin’ you can prepend $HOME/bin to your PATH and create an executable script $HOME/bin/grep containing the following:

#! /bin/sh
export PATH=/usr/bin
exec grep --color=auto --devices=skip "$@"