Environment Variables (GNU Grep 3.7)
Next: Exit Status, Previous: Command-line Options, Up: Invoking grep [Contents][Index]
2.2 Environment Variables
The behavior of grep is affected by the following environment variables.
The locale for category LC_foo /@w is specified by examining the three environment variables LC_ALL, LC_foo /@w , 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). The shell command locale -a lists locales that are currently available.
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_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 ofGREP_COLORShave 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-vcommand-line option is omitted, or a context line when-vis 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
terminfocapabilities 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
-vcommand-line option is omitted, or non-matching lines when-vis specified). If however the boolean ‘rv’ capability and the-vcommand-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
-vcommand-line option is omitted, or matching lines when-vis specified). If however the boolean ‘rv’ capability and the-vcommand-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-vcommand-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
-vcommand-line option is omitted, or a context line when-vis 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
-vcommand-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
-vcommand-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 theback_color_erase(bce) booleanterminfocapability 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
LANGThese variables specify the locale for the
LC_COLLATEcategory, which might affect how range expressions like ‘[a-z]’ are interpreted.LC_ALL¶
LC_CTYPE
LANGThese variables specify the locale for the
LC_CTYPEcategory, 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
LANGThese variables specify the locale for the
LC_MESSAGEScategory, which determines the language thatgrepuses for messages. The default ‘C’ locale uses American English messages.POSIXLY_CORRECT¶If set,
grepbehaves as POSIX requires; otherwise,grepbehaves 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_CORRECTdisables special handling of an invalid bracket expression. See invalid-bracket-expr._N_GNU_nonoption_argv_flags_¶(Here
Nisgrep’s numeric process ID.) If theith character of this environment variable’s value is ‘1’, do not consider theith operand ofgrepto 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 whenPOSIXLY_CORRECTis not set.
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 "$@"
Next: Exit Status, Previous: Command-line Options, Up: Invoking grep [Contents][Index]