<p><samp><span class="command">grep</span></samp> comes with a rich set of options from <span class="sc">posix.2</span> and <span class="sc">gnu</span>
extensions.
<dl>
<dt>`<samp><span class="samp">-c</span></samp>'<dt>`<samp><span class="samp">--count</span></samp>'<dd><a name="index-g_t_002dc-2"></a><a name="index-g_t_002d_002dcount-3"></a><a name="index-counting-lines-4"></a>Suppress normal output; instead print a count of matching
lines for each input file. With the `<samp><span class="samp">-v</span></samp>', `<samp><span class="samp">--invert-match</span></samp>' option,
count non-matching lines.
<br><dt>`<samp><span class="samp">-e </span><var>pattern</var></samp>'<dt>`<samp><span class="samp">--regexp=</span><var>pattern</var></samp>'<dd><a name="index-g_t_002de-5"></a><a name="index-g_t_002d_002dregexp_003d_0040var_007bpattern_007d-6"></a><a name="index-pattern-list-7"></a>Use <var>pattern</var> as the pattern; useful to protect patterns
beginning with a `<samp><span class="samp">-</span></samp>'.
<br><dt>`<samp><span class="samp">-f </span><var>file</var></samp>'<dt>`<samp><span class="samp">--file=</span><var>file</var></samp>'<dd><a name="index-g_t_002df-8"></a><a name="index-g_t_002d_002dfile-9"></a><a name="index-pattern-from-file-10"></a>Obtain patterns from <var>file</var>, one per line. The empty
file contains zero patterns, and therefore matches nothing.
<br><dt>`<samp><span class="samp">-i</span></samp>'<dt>`<samp><span class="samp">--ignore-case</span></samp>'<dd><a name="index-g_t_002di-11"></a><a name="index-g_t_002d_002dignore_002dcase-12"></a><a name="index-case-insensitive-search-13"></a>Ignore case distinctions in both the pattern and the input files.
<br><dt>`<samp><span class="samp">-l</span></samp>'<dt>`<samp><span class="samp">--files-with-matches</span></samp>'<dd><a name="index-g_t_002dl-14"></a><a name="index-g_t_002d_002dfiles_002dwith_002dmatches-15"></a><a name="index-names-of-matching-files-16"></a>Suppress normal output; instead print the name of each input
file from which output would normally have been printed.
The scanning of every file will stop on the first match.
<br><dt>`<samp><span class="samp">-n</span></samp>'<dt>`<samp><span class="samp">--line-number</span></samp>'<dd><a name="index-g_t_002dn-17"></a><a name="index-g_t_002d_002dline_002dnumber-18"></a><a name="index-line-numbering-19"></a>Prefix each line of output with the line number within its input file.
<br><dt>`<samp><span class="samp">-o</span></samp>'<dt>`<samp><span class="samp">--only-matching</span></samp>'<dd><a name="index-g_t_002do-20"></a><a name="index-g_t_002d_002donly_002dmatching-21"></a><a name="index-only-matching-22"></a>Print only the part of matching lines that actually matches <var>pattern</var>.
<br><dt>`<samp><span class="samp">-q</span></samp>'<dt>`<samp><span class="samp">--quiet</span></samp>'<dt>`<samp><span class="samp">--silent</span></samp>'<dd><a name="index-g_t_002dq-23"></a><a name="index-g_t_002d_002dquiet-24"></a><a name="index-g_t_002d_002dsilent-25"></a><a name="index-quiet_002c-silent-26"></a>Quiet; do not write anything to standard output. Exit immediately with
zero status if any match is found, even if an error was detected. Also
see the `<samp><span class="samp">-s</span></samp>' or `<samp><span class="samp">--no-messages</span></samp>' option.
<br><dt>`<samp><span class="samp">-s</span></samp>'<dt>`<samp><span class="samp">--no-messages</span></samp>'<dd><a name="index-g_t_002ds-27"></a><a name="index-g_t_002d_002dno_002dmessages-28"></a><a name="index-suppress-error-messages-29"></a>Suppress error messages about nonexistent or unreadable files.
Portability note: unlike <span class="sc">gnu</span> <samp><span class="command">grep</span></samp>, traditional
<samp><span class="command">grep</span></samp> did not conform to <span class="sc">posix.2</span>, because traditional
<samp><span class="command">grep</span></samp> lacked a `<samp><span class="samp">-q</span></samp>' option and its `<samp><span class="samp">-s</span></samp>' option behaved
to be portable to traditional <samp><span class="command">grep</span></samp> should avoid both
`<samp><span class="samp">-q</span></samp>' and `<samp><span class="samp">-s</span></samp>' and should redirect
output to <samp><span class="file">/dev/null</span></samp> instead.
<br><dt>`<samp><span class="samp">-v</span></samp>'<dt>`<samp><span class="samp">--invert-match</span></samp>'<dd><a name="index-g_t_002dv-30"></a><a name="index-g_t_002d_002dinvert_002dmatch-31"></a><a name="index-invert-matching-32"></a><a name="index-print-non_002dmatching-lines-33"></a>Invert the sense of matching, to select non-matching lines.
<br><dt>`<samp><span class="samp">-x</span></samp>'<dt>`<samp><span class="samp">--line-regexp</span></samp>'<dd><a name="index-g_t_002dx-34"></a><a name="index-g_t_002d_002dline_002dregexp-35"></a><a name="index-match-the-whole-line-36"></a>Select only those matches that exactly match the whole line.
<br><dt>`<samp><span class="samp">--colour[=</span><var>WHEN</var><span class="samp">]</span></samp>'<dt>`<samp><span class="samp">--color[=</span><var>WHEN</var><span class="samp">]</span></samp>'<dd><a name="index-g_t_002d_002dcolour-48"></a><a name="index-highlight_002c-color_002c-colour-49"></a>The matching string is surrounded by the marker specify in <var>GREP_COLOR</var>.
<var>WHEN</var> may be `never', `always', or `auto'.
<br><dt>`<samp><span class="samp">-</span><var>num</var></samp>'<dd><a name="index-g_t_002dNUM-50"></a>Same as `<samp><span class="samp">--context=</span><var>num</var></samp>' lines of leading and trailing
context. However, grep will never print any given line more than once.
<br><dt>`<samp><span class="samp">-V</span></samp>'<dt>`<samp><span class="samp">--version</span></samp>'<dd><a name="index-g_t_002dV-51"></a><a name="index-g_t_002d_002dversion-52"></a><a name="index-Version_002c-printing-53"></a>Print the version number of <samp><span class="command">grep</span></samp> to the standard output stream.
This version number should be included in all bug reports.
<br><dt>`<samp><span class="samp">--help</span></samp>'<dd><a name="index-g_t_002d_002dhelp-54"></a><a name="index-Usage-summary_002c-printing-55"></a>Print a usage message briefly summarizing these command-line options
and the bug-reporting address, then exit.
<dt>`<samp><span class="samp">--binary-files=</span><var>type</var></samp>'<dd><a name="index-g_t_002d_002dbinary_002dfiles-56"></a><a name="index-binary-files-57"></a>If the first few bytes of a file indicate that the file contains binary
data, assume that the file is of type <var>type</var>. By default,
<var>type</var> is `<samp><span class="samp">binary</span></samp>', and <samp><span class="command">grep</span></samp> normally outputs either
a one-line message saying that a binary file matches, or no message if
there is no match. If <var>type</var> is `<samp><span class="samp">without-match</span></samp>',
<samp><span class="command">grep</span></samp> assumes that a binary file does not match;
this is equivalent to the `<samp><span class="samp">-I</span></samp>' option. If <var>type</var>
is `<samp><span class="samp">text</span></samp>', <samp><span class="command">grep</span></samp> processes a binary file as if it were
text; this is equivalent to the `<samp><span class="samp">-a</span></samp>' option.
which can have nasty side effects if the output is a terminal and if the
terminal driver interprets some of it as commands.
<br><dt>`<samp><span class="samp">-b</span></samp>'<dt>`<samp><span class="samp">--byte-offset</span></samp>'<dd><a name="index-g_t_002db-58"></a><a name="index-g_t_002d_002dbyte_002doffset-59"></a><a name="index-byte-offset-60"></a>Print the byte offset within the input file before each line of output.
When <samp><span class="command">grep</span></samp> runs on <span class="sc">ms-dos</span> or MS-Windows, the printed
byte offsets
depend on whether the `<samp><span class="samp">-u</span></samp>' (`<samp><span class="samp">--unix-byte-offsets</span></samp>') option is
used; see below.
<br><dt>`<samp><span class="samp">-D </span><var>action</var></samp>'<dt>`<samp><span class="samp">--devices=</span><var>action</var></samp>'<dd><a name="index-g_t_002dD-61"></a><a name="index-g_t_002d_002ddevices-62"></a><a name="index-device-search-63"></a>If an input file is a device, FIFO or socket, use <var>action</var> to process it.
By default, <var>action</var> is `<samp><span class="samp">read</span></samp>', which means that devices are
read just as if they were ordinary files.
If <var>action</var> is `<samp><span class="samp">skip</span></samp>', devices, FIFOs and sockets are silently
skipped.
<br><dt>`<samp><span class="samp">-d </span><var>action</var></samp>'<dt>`<samp><span class="samp">--directories=</span><var>action</var></samp>'<dd><a name="index-g_t_002dd-64"></a><a name="index-g_t_002d_002ddirectories-65"></a><a name="index-directory-search-66"></a>If an input file is a directory, use <var>action</var> to process it.
By default, <var>action</var> is `<samp><span class="samp">read</span></samp>', which means that directories are
read just as if they were ordinary files (some operating systems
and filesystems disallow this, and will cause <samp><span class="command">grep</span></samp> to print error
messages for every directory or silently skip them). If <var>action</var> is
`<samp><span class="samp">skip</span></samp>', directories are silently skipped. If <var>action</var> is
`<samp><span class="samp">recurse</span></samp>', <samp><span class="command">grep</span></samp> reads all files under each directory,
recursively; this is equivalent to the `<samp><span class="samp">-r</span></samp>' option.
<br><dt>`<samp><span class="samp">-H</span></samp>'<dt>`<samp><span class="samp">--with-filename</span></samp>'<dd><a name="index-g_t_002dH-67"></a><a name="index-g_t_002d_002dWith_002dfilename-68"></a><a name="index-with-filename-prefix-69"></a>Print the filename for each match.
<br><dt>`<samp><span class="samp">-h</span></samp>'<dt>`<samp><span class="samp">--no-filename</span></samp>'<dd><a name="index-g_t_002dh-70"></a><a name="index-g_t_002d_002dno_002dfilename-71"></a><a name="index-no-filename-prefix-72"></a>Suppress the prefixing of filenames on output when multiple files are searched.
<br><dt>`<samp><span class="samp">--line-buffered</span></samp>'<dd><a name="index-g_t_002d_002dline_002dbuffered-73"></a><a name="index-line-buffering-74"></a>Set the line buffering policy, this can be a performance penality.
<br><dt>`<samp><span class="samp">--label=</span><var>LABEL</var></samp>'<dd><a name="index-g_t_002d_002dlabel-75"></a><a name="index-changing-name-of-standard-input-76"></a>Displays input actually coming from standard input as input coming from file
<var>LABEL</var>. This is especially useful for tools like zgrep, e.g.
<br><dt>`<samp><span class="samp">-L</span></samp>'<dt>`<samp><span class="samp">--files-without-match</span></samp>'<dd><a name="index-g_t_002dL-77"></a><a name="index-g_t_002d_002dfiles_002dwithout_002dmatch-78"></a><a name="index-files-which-don_0027t-match-79"></a>Suppress normal output; instead print the name of each input
file from which no output would normally have been printed.
The scanning of every file will stop on the first match.
<br><dt>`<samp><span class="samp">-a</span></samp>'<dt>`<samp><span class="samp">--text</span></samp>'<dd><a name="index-g_t_002da-80"></a><a name="index-g_t_002d_002dtext-81"></a><a name="index-suppress-binary-data-82"></a><a name="index-binary-files-83"></a>Process a binary file as if it were text; this is equivalent to the
<br><dt>`<samp><span class="samp">-I</span></samp>'<dd>Process a binary file as if it did not contain matching data; this is
equivalent to the `<samp><span class="samp">--binary-files=without-match</span></samp>' option.
<br><dt>`<samp><span class="samp">-w</span></samp>'<dt>`<samp><span class="samp">--word-regexp</span></samp>'<dd><a name="index-g_t_002dw-84"></a><a name="index-g_t_002d_002dword_002dregexp-85"></a><a name="index-matching-whole-words-86"></a>Select only those lines containing matches that form
whole words. The test is that the matching substring
must either be at the beginning of the line, or preceded
by a non-word constituent character. Similarly,
it must be either at the end of the line or followed by
a non-word constituent character. Word-constituent
characters are letters, digits, and the underscore.
<br><dt>`<samp><span class="samp">-r</span></samp>'<dt>`<samp><span class="samp">-R</span></samp>'<dt>`<samp><span class="samp">--recursive</span></samp>'<dd><a name="index-g_t_002dr-87"></a><a name="index-g_t_002d_002drecursive-88"></a><a name="index-recursive-search-89"></a><a name="index-searching-directory-trees-90"></a>For each directory mentioned in the command line, read and process all
files in that directory, recursively. This is the same as the
<br><dt>`<samp><span class="samp">-m </span><var>num</var></samp>'<dt>`<samp><span class="samp">--max-count=</span><var>num</var></samp>'<dd><a name="index-g_t_002dm-97"></a><a name="index-g_t_002d_002dmax_002dcount-98"></a><a name="index-max_002dcount-99"></a>Stop reading a file after <var>num</var> matching lines. If the input is
standard input from a regular file, and <var>num</var> matching lines are
output, <samp><span class="command">grep</span></samp> ensures that the standard input is positioned to
just after the last matching line before exiting, regardless of the
presence of trailing context lines. This enables a calling process
to resume a search. For example, the following shell script makes use
of it:
<pre class="example"> while grep -m 1 PATTERN
do
echo xxxx
done < FILE
</pre>
<p>But the following probably will not work because a pipe is not a regular
file:
<pre class="example"> # This probably will not work.
cat FILE |
while grep -m 1 PATTERN
do
echo xxxx
done
</pre>
<p>When <samp><span class="command">grep</span></samp> stops after NUM matching lines, it outputs
any trailing context lines. Since context does not include matching
lines, <samp><span class="command">grep</span></samp> will stop when it encounters another matching line.
When the `<samp><span class="samp">-c</span></samp>' or `<samp><span class="samp">--count</span></samp>' option is also used,
<samp><span class="command">grep</span></samp> does not output a count greater than <var>num</var>.
When the `<samp><span class="samp">-v</span></samp>' or `<samp><span class="samp">--invert-match</span></samp>' option is
also used, <samp><span class="command">grep</span></samp> stops after outputting <var>num</var>
non-matching lines.
<br><dt>`<samp><span class="samp">-y</span></samp>'<dd><a name="index-g_t_002dy-100"></a><a name="index-case-insensitive-search_002c-obsolete-option-101"></a>Obsolete synonym for `<samp><span class="samp">-i</span></samp>'.
<br><dt>`<samp><span class="samp">-U</span></samp>'<dt>`<samp><span class="samp">--binary</span></samp>'<dd><a name="index-g_t_002dU-102"></a><a name="index-g_t_002d_002dbinary-103"></a><a name="index-DOS_002fWindows-binary-files-104"></a><a name="index-binary-files_002c-DOS_002fWindows-105"></a>Treat the file(s) as binary. By default, under <span class="sc">ms-dos</span>
and MS-Windows, <samp><span class="command">grep</span></samp> guesses the file type by looking
at the contents of the first 32kB read from the file.
If <samp><span class="command">grep</span></samp> decides the file is a text file, it strips the
<code>CR</code> characters from the original file contents (to make
regular expressions with <code>^</code> and <code>$</code> work correctly).
Specifying `<samp><span class="samp">-U</span></samp>' overrules this guesswork, causing all
files to be read and passed to the matching mechanism
verbatim; if the file is a text file with <code>CR/LF</code> pairs
at the end of each line, this will cause some regular
expressions to fail. This option has no effect on platforms other than
<samp><span class="command">grep</span></samp> to report byte offsets as if the file were Unix style
text file, i.e., the byte offsets ignore the <code>CR</code> characters which were
stripped. This will produce results identical to running <samp><span class="command">grep</span></samp> on
a Unix machine. This option has no effect unless `<samp><span class="samp">-b</span></samp>'
option is also used; it has no effect on platforms other than <span class="sc">ms-dos</span> and
MS-Windows.
<br><dt>`<samp><span class="samp">--mmap</span></samp>'<dd><a name="index-g_t_002d_002dmmap-110"></a><a name="index-memory-mapped-input-111"></a>If possible, use the <code>mmap</code> system call to read input, instead of
the default <code>read</code> system call. In some situations, `<samp><span class="samp">--mmap</span></samp>'
yields better performance. However, `<samp><span class="samp">--mmap</span></samp>' can cause undefined
behavior (including core dumps) if an input file shrinks while
<samp><span class="command">grep</span></samp> is operating, or if an I/O error occurs.
<br><dt>`<samp><span class="samp">-Z</span></samp>'<dt>`<samp><span class="samp">--null</span></samp>'<dd><a name="index-g_t_002dZ-112"></a><a name="index-g_t_002d_002dnull-113"></a><a name="index-zero_002dterminated-file-names-114"></a>Output a zero byte (the <span class="sc">ascii</span> <code>NUL</code> character) instead of the
character that normally follows a file name. For example, `<samp><span class="samp">grep
-lZ</span></samp>' outputs a zero byte after each file name instead of the usual
newline. This option makes the output unambiguous, even in the presence
of file names containing unusual characters like newlines. This option
can be used with commands like `<samp><span class="samp">find -print0</span></samp>', `<samp><span class="samp">perl -0</span></samp>',
`<samp><span class="samp">sort -z</span></samp>', and `<samp><span class="samp">xargs -0</span></samp>' to process arbitrary file names,
even those that contain newline characters.
<br><dt>`<samp><span class="samp">-z</span></samp>'<dt>`<samp><span class="samp">--null-data</span></samp>'<dd><a name="index-g_t_002dz-115"></a><a name="index-g_t_002d_002dnull_002ddata-116"></a><a name="index-zero_002dterminated-lines-117"></a>Treat the input as a set of lines, each terminated by a zero byte (the
<span class="sc">ascii</span> <code>NUL</code> character) instead of a newline. Like the `<samp><span class="samp">-Z</span></samp>'
or `<samp><span class="samp">--null</span></samp>' option, this option can be used with commands like
`<samp><span class="samp">sort -z</span></samp>' to process arbitrary file names.
</dl>
<p>Several additional options control which variant of the <samp><span class="command">grep</span></samp>
matching engine is used. See <a href="#Grep-Programs">Grep Programs</a>.
<p>Grep's behavior is affected by the following environment variables.
<p>A locale <code>LC_</code><var>foo</var> is specified by examining the three
environment variables <samp><span class="env">LC_ALL</span></samp>, <samp><span class="env">LC_</span><var>foo</var></samp>, and <samp><span class="env">LANG</span></samp>,
in that order. The first of these variables that is set specifies the
locale. For example, if <samp><span class="env">LC_ALL</span></samp> is not set, but <samp><span class="env">LC_MESSAGES</span></samp>
is set to `<samp><span class="samp">pt_BR</span></samp>', then Brazilian Portuguese is used for the
<code>LC_MESSAGES</code> locale. The C locale is used if none of these
environment variables are set, or if the locale catalog is not
installed, or if <samp><span class="command">grep</span></samp> was not compiled with national language
support (<span class="sc">nls</span>).
<p><a name="index-environment-variables-118"></a>
<dl>
<dt><samp><span class="env">GREP_OPTIONS</span></samp><dd><a name="index-GREP_005fOPTIONS-119"></a><a name="index-default-options-environment-variable-120"></a>This variable specifies default options to be placed in front of any
explicit options. For example, if <code>GREP_OPTIONS</code> is
behaves as if the two options `<samp><span class="samp">--binary-files=without-match</span></samp>' and
`<samp><span class="samp">--directories=skip</span></samp>' had been specified before
any explicit options. Option specifications are separated by
whitespace. A backslash escapes the next character, so it can be used to
specify an option containing whitespace or a backslash.
<br><dt><samp><span class="env">GREP_COLOR</span></samp><dd><a name="index-GREP_005fCOLOR-121"></a><a name="index-highlight-markers-122"></a>This variable specifies the surrounding markers use to highlight the matching
text. The default is control ascii red.
<br><dt><samp><span class="env">LC_ALL</span></samp><dt><samp><span class="env">LC_COLLATE</span></samp><dt><samp><span class="env">LANG</span></samp><dd><a name="index-LC_005fALL-123"></a><a name="index-LC_005fCOLLATE-124"></a><a name="index-LANG-125"></a><a name="index-character-type-126"></a><a name="index-national-language-support-127"></a><a name="index-NLS-128"></a>These variables specify the <code>LC_COLLATE</code> locale, which determines
the collating sequence used to interpret range expressions like
`<samp><span class="samp">[a-z]</span></samp>'.
<br><dt><samp><span class="env">LC_ALL</span></samp><dt><samp><span class="env">LC_CTYPE</span></samp><dt><samp><span class="env">LANG</span></samp><dd><a name="index-LC_005fALL-129"></a><a name="index-LC_005fCTYPE-130"></a><a name="index-LANG-131"></a><a name="index-character-type-132"></a><a name="index-national-language-support-133"></a><a name="index-NLS-134"></a>These variables specify the <code>LC_CTYPE</code> locale, which determines the
type of characters, e.g., which characters are whitespace.
<br><dt><samp><span class="env">LC_ALL</span></samp><dt><samp><span class="env">LC_MESSAGES</span></samp><dt><samp><span class="env">LANG</span></samp><dd><a name="index-LC_005fALL-135"></a><a name="index-LC_005fMESSAGES-136"></a><a name="index-LANG-137"></a><a name="index-language-of-messages-138"></a><a name="index-message-language-139"></a><a name="index-national-language-support-140"></a><a name="index-NLS-141"></a><a name="index-translation-of-message-language-142"></a>These variables specify the <code>LC_MESSAGES</code> locale, which determines
the language that <samp><span class="command">grep</span></samp> uses for messages. The default C
<samp><span class="command">grep</span></samp> behaves more like other <span class="sc">gnu</span> programs. <span class="sc">posix.2</span>
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, <span class="sc">posix.2</span> requires that unrecognized options be
diagnosed as
“illegal”, but since they are not really against the law the default
is to diagnose them as “invalid”. <code>POSIXLY_CORRECT</code> also
disables <code>_</code><var>N</var><code>_GNU_nonoption_argv_flags_</code>, described below.
<br><dt><samp><span class="env">_</span><var>N</var><span class="env">_GNU_nonoption_argv_flags_</span></samp><dd><a name="index-g_t_005f_0040var_007bN_007d_005fGNU_005fnonoption_005fargv_005fflags_005f-144"></a>(Here <var>N</var> is <samp><span class="command">grep</span></samp>'s numeric process ID.) If the
<var>i</var>th character of this environment variable's value is `<samp><span class="samp">1</span></samp>', do
not consider the <var>i</var>th operand of <samp><span class="command">grep</span></samp> 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 <span class="sc">gnu</span> C library, and
only when <code>POSIXLY_CORRECT</code> is not set.
<p><samp><span class="command">grep</span></samp> searches the named input files (or standard input if no
files are named, or the file name <samp><span class="file">-</span></samp> is given) for lines containing
a match to the given pattern. By default, <samp><span class="command">grep</span></samp> prints the
matching lines. There are four major variants of <samp><span class="command">grep</span></samp>,
controlled by the following options.
<dl>
<dt>`<samp><span class="samp">-G</span></samp>'<dt>`<samp><span class="samp">--basic-regexp</span></samp>'<dd><a name="index-g_t_002dG-145"></a><a name="index-g_t_002d_002dbasic_002dregexp-146"></a><a name="index-matching-basic-regular-expressions-147"></a>Interpret the pattern as a basic regular expression. This is the default.
<br><dt>`<samp><span class="samp">-E</span></samp>'<dt>`<samp><span class="samp">--extended-regexp</span></samp>'<dd><a name="index-g_t_002dE-148"></a><a name="index-g_t_002d_002dextended_002dregexp-149"></a><a name="index-matching-extended-regular-expressions-150"></a>Interpret the pattern as an extended regular expression.
<br><dt>`<samp><span class="samp">-F</span></samp>'<dt>`<samp><span class="samp">--fixed-strings</span></samp>'<dd><a name="index-g_t_002dF-151"></a><a name="index-g_t_002d_002dfixed_002dstrings-152"></a><a name="index-matching-fixed-strings-153"></a>Interpret the pattern as a list of fixed strings, separated
by newlines, any of which is to be matched.
<br><dt>`<samp><span class="samp">-P</span></samp>'<dt>`<samp><span class="samp">--perl-regexp</span></samp>'<dd><a name="index-g_t_002dP-154"></a><a name="index-g_t_002d_002dperl_002dregexp-155"></a><a name="index-matching-Perl-regular-expressions-156"></a>Interpret the pattern as a Perl regular expression.
</dl>
<p>In addition, two variant programs <span class="sc">egrep</span> and <span class="sc">fgrep</span> are available.
<span class="sc">egrep</span> is the same as `<samp><span class="samp">grep -E</span></samp>'. <span class="sc">fgrep</span> is the
same as `<samp><span class="samp">grep -F</span></samp>'.
A <dfn>regular expression</dfn> is a pattern that describes a set of strings.
Regular expressions are constructed analogously to arithmetic expressions,
by using various operators to combine smaller expressions.
<samp><span class="command">grep</span></samp> understands two different versions of regular expression
syntax: “basic”(BRE) and “extended”(ERE). In <span class="sc">gnu</span> <samp><span class="command">grep</span></samp>,
there is no difference in available functionality using either syntax.
In other implementations, basic regular expressions are less powerful.
The following description applies to extended regular expressions;
differences for basic regular expressions are summarized afterwards.
<p>The fundamental building blocks are the regular expressions that match
a single character. Most characters, including all letters and digits,
are regular expressions that match themselves. Any metacharacter
with special meaning may be quoted by preceding it with a backslash.
<p>A regular expression may be followed by one of several
repetition operators:
<dl>
<dt>`<samp><span class="samp">.</span></samp>'<dd><a name="index-g_t_002e-158"></a><a name="index-dot-159"></a><a name="index-period-160"></a>The period `<samp><span class="samp">.</span></samp>' matches any single character.
<br><dt>`<samp><span class="samp">?</span></samp>'<dd><a name="index-g_t_003f-161"></a><a name="index-question-mark-162"></a><a name="index-match-sub_002dexpression-at-most-once-163"></a>The preceding item is optional and will be matched at most once.
<br><dt>`<samp><span class="samp">*</span></samp>'<dd><a name="index-g_t_002a-164"></a><a name="index-asterisk-165"></a><a name="index-match-sub_002dexpression-zero-or-more-times-166"></a>The preceding item will be matched zero or more times.
<br><dt>`<samp><span class="samp">+</span></samp>'<dd><a name="index-g_t_002b-167"></a><a name="index-plus-sign-168"></a>The preceding item will be matched one or more times.
<br><dt>`<samp><span class="samp">{</span><var>n</var><span class="samp">,}</span></samp>'<dd><a name="index-g_t_0040_007bn_002c_0040_007d-172"></a><a name="index-braces_002c-second-argument-omitted-173"></a><a name="index-match-sub_002dexpression-n-or-more-times-174"></a>The preceding item is matched n or more times.
<br><dt>`<samp><span class="samp">{</span><var>n</var><span class="samp">,</span><var>m</var><span class="samp">}</span></samp>'<dd><a name="index-g_t_0040_007bn_002cm_0040_007d-175"></a><a name="index-braces_002c-two-arguments-176"></a>The preceding item is matched at least <var>n</var> times, but not more than
<var>m</var> times.
</dl>
<p>Two regular expressions may be concatenated; the resulting regular
expression matches any string formed by concatenating two substrings
that respectively match the concatenated subexpressions.
<p>Two regular expressions may be joined by the infix operator `<samp><span class="samp">|</span></samp>'; the
resulting regular expression matches any string matching either subexpression.
<p>Repetition takes precedence over concatenation, which in turn
takes precedence over alternation. A whole subexpression may be
enclosed in parentheses to override these precedence rules.
<h3 class="section">5.1 Character Class</h3>
<p><a name="index-bracket-expression-177"></a><a name="index-character-class-178"></a>A <dfn>bracket expression</dfn> is a list of characters enclosed by `<samp><span class="samp">[</span></samp>' and
`<samp><span class="samp">]</span></samp>'. It matches any single character in that list; if the first
character of the list is the caret `<samp><span class="samp">^</span></samp>', then it matches any character
<strong>not</strong> in the list. For example, the regular expression
`<samp><span class="samp">[0123456789]</span></samp>' matches any single digit.
<p><a name="index-range-expression-179"></a>Within a bracket expression, a <dfn>range expression</dfn> consists of two
characters separated by a hyphen. It matches any single character that
sorts between the two characters, inclusive, using the locale's
collating sequence and character set. For example, in the default C
locale, `<samp><span class="samp">[a-d]</span></samp>' is equivalent to `<samp><span class="samp">[abcd]</span></samp>'. Many locales sort
characters in dictionary order, and in these locales `<samp><span class="samp">[a-d]</span></samp>' is
typically not equivalent to `<samp><span class="samp">[abcd]</span></samp>'; it might be equivalent to
`<samp><span class="samp">[aBbCcDd]</span></samp>', for example. To obtain the traditional interpretation
of bracket expressions, you can use the C locale by setting the
<samp><span class="env">LC_ALL</span></samp> environment variable to the value `<samp><span class="samp">C</span></samp>'.
<p>Finally, certain named classes of characters are predefined within
bracket expressions, as follows.
Their interpretation depends on the <code>LC_CTYPE</code> locale; the
interpretation below is that of the C locale, which is the default
<br><dt>`<samp><span class="samp">[:cntrl:]</span></samp>'<dd><a name="index-cntrl-188"></a><a name="index-control-characters-189"></a>Control characters. In <span class="sc">ascii</span>, these characters have octal codes 000
through 037, and 177 (<code>DEL</code>). In other character sets, these are
<code>0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f</code>.
</dl>
For example, `<samp><span class="samp">[[:alnum:]]</span></samp>' means `<samp><span class="samp">[0-9A-Za-z]</span></samp>', except the latter
depends upon the C locale and the <span class="sc">ascii</span> character
encoding, whereas the former is independent of locale and character set.
(Note that the brackets in these class names are
part of the symbolic names, and must be included in addition to
the brackets delimiting the bracket list.)
<p>Most metacharacters lose their special meaning inside lists.
<dl>
<dt>`<samp><span class="samp">]</span></samp>'<dd>ends the list if it's not the first list item. So, if you want to make
the `<samp><span class="samp">]</span></samp>' character a list item, you must put it first.
<br><dt>`<samp><span class="samp">[.</span></samp>'<dd>represents the open collating symbol.
<br><dt>`<samp><span class="samp">.]</span></samp>'<dd>represents the close collating symbol.
<br><dt>`<samp><span class="samp">[=</span></samp>'<dd>represents the open equivalence class.
<br><dt>`<samp><span class="samp">=]</span></samp>'<dd>represents the close equivalence class.
<br><dt>`<samp><span class="samp">[:</span></samp>'<dd>represents the open character class followed by a valid character class name.
<br><dt>`<samp><span class="samp">:]</span></samp>'<dd>represents the close character class followed by a valid character class name.
<br><dt>`<samp><span class="samp">-</span></samp>'<dd>represents the range if it's not first or last in a list or the ending point
of a range.
<br><dt>`<samp><span class="samp">^</span></samp>'<dd>represents the characters not in the list. If you want to make the `<samp><span class="samp">^</span></samp>'
character a list item, place it anywhere but first.
</dl>
<h3 class="section">5.2 Backslash Character</h3>
<p><a name="index-backslash-209"></a>
The `<samp><span class="samp">\</span></samp>' when followed by certain ordinary characters take a special
meaning :
<dl>
<dt>`<samp><span class="samp">`<samp></span><span class="samp">\b</span><span class="samp"></samp>'</span></samp>'<dd>Match the empty string at the edge of a word.
<br><dt>`<samp><span class="samp">`<samp></span><span class="samp">\B</span><span class="samp"></samp>'</span></samp>'<dd>Match the empty string provided it's not at the edge of a word.
<br><dt>`<samp><span class="samp">`<samp></span><span class="samp">\<</span><span class="samp"></samp>'</span></samp>'<dd>Match the empty string at the beginning of word.
<br><dt>`<samp><span class="samp">`<samp></span><span class="samp">\></span><span class="samp"></samp>'</span></samp>'<dd>Match the empty string at the end of word.
<br><dt>`<samp><span class="samp">`<samp></span><span class="samp">\w</span><span class="samp"></samp>'</span></samp>'<dd>Match word constituent, it is a synonym for `<samp><span class="samp">[[:alnum:]]</span></samp>'.
<br><dt>`<samp><span class="samp">`<samp></span><span class="samp">\W</span><span class="samp"></samp>'</span></samp>'<dd>Match non word constituent, it is a synonym for `<samp><span class="samp">[^[:alnum:]]</span></samp>'.
</dl>
<p>For example , `<samp><span class="samp">\brat\b</span></samp>' matches the separate word `<samp><span class="samp">rat</span></samp>',
`<samp><span class="samp">c\Brat\Be</span></samp>' matches `<samp><span class="samp">crate</span></samp>', but `<samp><span class="samp">dirty \Brat</span></samp>' doesn't
match `<samp><span class="samp">dirty rat</span></samp>'.
<h3 class="section">5.3 Anchoring</h3>
<p><a name="index-anchoring-210"></a>
The caret `<samp><span class="samp">^</span></samp>' and the dollar sign `<samp><span class="samp">$</span></samp>' are metacharacters that
respectively match the empty string at the beginning and end of a line.
<h3 class="section">5.4 Back-reference</h3>
<p><a name="index-back_002dreference-211"></a>
The back-reference `<samp><span class="samp">\</span><var>n</var></samp>', where <var>n</var> is a single digit, matches
the substring previously matched by the <var>n</var>th parenthesized subexpression
of the regular expression. For example, `<samp><span class="samp">(a)\1</span></samp>' matches `<samp><span class="samp">aa</span></samp>'.
When use with alternation if the group does not participate in the match, then
the back-reference makes the whole match fail. For example, `<samp><span class="samp">a(.)|b\1</span></samp>'
will not match `<samp><span class="samp">ba</span></samp>'. When multiple regular expressions are given with
`<samp><span class="samp">-e</span></samp>' or from a file `<samp><span class="samp">-f file</span></samp>', the back-referecences are local to
In basic regular expressions the metacharacters `<samp><span class="samp">?</span></samp>', `<samp><span class="samp">+</span></samp>',
`<samp><span class="samp">{</span></samp>', `<samp><span class="samp">|</span></samp>', `<samp><span class="samp">(</span></samp>', and `<samp><span class="samp">)</span></samp>' lose their special meaning;
instead use the backslashed versions `<samp><span class="samp">\?</span></samp>', `<samp><span class="samp">\+</span></samp>', `<samp><span class="samp">\{</span></samp>',
`<samp><span class="samp">\|</span></samp>', `<samp><span class="samp">\(</span></samp>', and `<samp><span class="samp">\)</span></samp>'.
<p><a name="index-interval-specifications-213"></a>Traditional <samp><span class="command">egrep</span></samp> did not support the `<samp><span class="samp">{</span></samp>' metacharacter,
and some <samp><span class="command">egrep</span></samp> implementations support `<samp><span class="samp">\{</span></samp>' instead, so
portable scripts should avoid `<samp><span class="samp">{</span></samp>' in `<samp><span class="samp">egrep</span></samp>' patterns and
should use `<samp><span class="samp">[{]</span></samp>' to match a literal `<samp><span class="samp">{</span></samp>'.
<p><span class="sc">gnu</span> <samp><span class="command">egrep</span></samp> attempts to support traditional usage by
assuming that `<samp><span class="samp">{</span></samp>' is not special if it would be the start of an
invalid interval specification. For example, the shell command
`<samp><span class="samp">egrep '{1'</span></samp>' searches for the two-character string `<samp><span class="samp">{1</span></samp>'
instead of reporting a syntax error in the regular expression.
<span class="sc">posix.2</span> allows this behavior as an extension, but portable scripts
<p><a name="index-Usage_002c-examples-214"></a>Here is an example shell command that invokes <span class="sc">gnu</span> <samp><span class="command">grep</span></samp>:
<p class="noindent">This lists all lines in the files <samp><span class="file">menu.h</span></samp> and <samp><span class="file">main.c</span></samp> that
contain the string `<samp><span class="samp">hello</span></samp>' followed by the string `<samp><span class="samp">world</span></samp>';
this is because `<samp><span class="samp">.*</span></samp>' matches zero or more characters within a line.
See <a href="#Regular-Expressions">Regular Expressions</a>. The `<samp><span class="samp">-i</span></samp>' option causes <samp><span class="command">grep</span></samp>
to ignore case, causing it to match the line `<samp><span class="samp">Hello, world!</span></samp>', which
it would not otherwise match. See <a href="#Invoking">Invoking</a>, for more details about
how to invoke <samp><span class="command">grep</span></samp>.
<p><a name="index-Using-_0040command_007bgrep_007d_002c-Q_0026A-215"></a><a name="index-FAQ-about-_0040command_007bgrep_007d-usage-216"></a>Here are some common questions and answers about <samp><span class="command">grep</span></samp> usage.
<ol type=1 start=1>
<li>How can I list just the names of matching files?
<pre class="example"> grep -l 'main' *.c
</pre>
<p class="noindent">lists the names of all C files in the current directory whose contents
<p class="noindent">searches for `<samp><span class="samp">hello</span></samp>' in all files under the directory
<samp><span class="file">/home/gigi</span></samp>. For more control of which files are searched, use
<samp><span class="command">find</span></samp>, <samp><span class="command">grep</span></samp> and <samp><span class="command">xargs</span></samp>. For example,
<p>which merely looks for `<samp><span class="samp">hello</span></samp>' in all files in the current
directory whose names end in `<samp><span class="samp">.c</span></samp>'. Here the <samp><span class="option">-r</span></samp> is
probably unnecessary, as recursion occurs only in the unlikely event
that one of `<samp><span class="samp">.c</span></samp>' files is a directory.
<li>What if a pattern has a leading `<samp><span class="samp">-</span></samp>'?
<pre class="example"> grep -e '--cut here--' *
</pre>
<p class="noindent">searches for all lines matching `<samp><span class="samp">--cut here--</span></samp>'. Without `<samp><span class="samp">-e</span></samp>',
<samp><span class="command">grep</span></samp> would attempt to parse `<samp><span class="samp">--cut here--</span></samp>' as a list of
options.
<li>Suppose I want to search for a whole word, not a part of a word?
<pre class="example"> grep -w 'hello' *
</pre>
<p class="noindent">searches only for instances of `<samp><span class="samp">hello</span></samp>' that are entire words; it
does not match `<samp><span class="samp">Othello</span></samp>'. For more control, use `<samp><span class="samp">\<</span></samp>' and
`<samp><span class="samp">\></span></samp>' to match the start and end of words. For example:
<pre class="example"> grep 'hello\>' *
</pre>
<p class="noindent">searches only for words ending in `<samp><span class="samp">hello</span></samp>', so it matches the word
`<samp><span class="samp">Othello</span></samp>'.
<li>How do I output context around the matching lines?
<pre class="example"> grep -C 2 'hello' *
</pre>
<p class="noindent">prints two lines of context around each matching line.
<li>How do I force grep to print the name of the file?
<p class="noindent">finds all lines that contain both `<samp><span class="samp">paul</span></samp>' and `<samp><span class="samp">franc,ois</span></samp>'.
<li>How can I search in both standard input and in files?
<p>Use the special file name `<samp><span class="samp">-</span></samp>':
<li><a href="#index-files-which-don_0027t-match-79">files which don't match</a>: <a href="#Invoking">Invoking</a></li>
<li><a href="#index-GPL_002c-GNU-General-Public-License-220">GPL, GNU General Public License</a>: <a href="#GNU-General-Public-License">GNU General Public License</a></li>
<li><a href="#index-match-sub_002dexpression-at-most-once-163">match sub-expression at most once</a>: <a href="#Regular-Expressions">Regular Expressions</a></li>
<li><a href="#index-match-sub_002dexpression-n-or-more-times-174">match sub-expression n or more times</a>: <a href="#Regular-Expressions">Regular Expressions</a></li>
<li><a href="#index-match-sub_002dexpression-n-times-171">match sub-expression n times</a>: <a href="#Regular-Expressions">Regular Expressions</a></li>
<li><a href="#index-match-sub_002dexpression-zero-or-more-times-166">match sub-expression zero or more times</a>: <a href="#Regular-Expressions">Regular Expressions</a></li>
<li><a href="#index-match-the-whole-line-36">match the whole line</a>: <a href="#Invoking">Invoking</a></li>