Next: , Previous: Command-line Options, Up: Invoking


2.2 Environment Variables

The behavior of grep is affected by the following environment variables.

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’, then the 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).

Many of the environment variables in the following list let you 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.

The two-letter names used in the GREP_COLORS environment variable (and some of the others) 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.

GREP_OPTIONS
This variable specifies default options to be placed in front of any explicit options. As this causes problems when writing portable scripts, this feature will be removed in a future release of grep, and grep warns if it is used. Please use an alias or script instead.
GREP_COLOR
This variable specifies the color used to highlight matched (non-empty) text. It is deprecated in favor of GREP_COLORS, but still supported. The ‘mt’, ‘ms’, and ‘mc’ capabilities of GREP_COLORS have priority over it. It can only specify the color used to highlight the matching non-empty text in any matching line (a selected line when the -v command-line option is omitted, or a context line when -v is specified). The default is ‘01;31’, which means a bold red foreground text on the terminal's default background.
GREP_COLORS
This variable specifies the colors and other attributes used to highlight various parts of the 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). 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.
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. Also, POSIXLY_CORRECT disables special handling of an invalid bracket expression. See invalid-bracket-expr.
_N_GNU_nonoption_argv_flags_
(Here N is grep's numeric process ID.) If the ith character of this environment variable's value is ‘1’, do not consider the ith operand of grep to be an option, even if it appears to be one. A shell can put this variable in the environment for each command it runs, specifying which operands are the results of file name wildcard expansion and therefore should not be treated as options. This behavior is available only with the GNU C library, and only when POSIXLY_CORRECT is not set.