grep utils: Update from version 2.12 to 2.14
[dragonfly.git] / gnu / usr.bin / grep / grep / grep.1
1 .\" GNU grep man page
2 .if !\n(.g \{\
3 .       if !\w|\*(lq| \{\
4 .               ds lq ``
5 .               if \w'\(lq' .ds lq "\(lq
6 .       \}
7 .       if !\w|\*(rq| \{\
8 .               ds rq ''
9 .               if \w'\(rq' .ds rq "\(rq
10 .       \}
11 .\}
12 .ie t .ds Tx \s-1T\v'.4n'\h'-.1667'E\v'-.4n'\h'-.125'X\s0
13 . el  .ds Tx TeX
14 .de Id
15 . ds Yr \\$4
16 . substring Yr 0 3
17 . ds Mn \\$4
18 . substring Mn 5 6
19 . ds Dy \\$4
20 . substring Dy 8 9
21 . \" ISO 8601 date, complete format, extended representation
22 . ds Dt \\*(Yr-\\*(Mn-\\*(Dy
23 ..
24 .TH GREP 1 \*(Dt "GNU grep 2.14" "User Commands"
25 .hy 0
26 .
27 .SH NAME
28 grep, egrep, fgrep \- print lines matching a pattern
29 .
30 .SH SYNOPSIS
31 .B grep
32 .RI [ OPTIONS ]
33 .I PATTERN
34 .RI [ FILE .\|.\|.]
35 .br
36 .B grep
37 .RI [ OPTIONS ]
38 .RB [ \-e
39 .I PATTERN
40 |
41 .B \-f
42 .IR FILE ]
43 .RI [ FILE .\|.\|.]
44 .
45 .SH DESCRIPTION
46 .B grep
47 searches the named input
48 .IR FILE s
49 (or standard input if no files are named,
50 or if a single hyphen-minus
51 .RB ( \- )
52 is given as file name)
53 for lines containing a match to the given
54 .IR PATTERN .
55 By default,
56 .B grep
57 prints the matching lines.
58 .PP
59 In addition, two variant programs
60 .B egrep
61 and
62 .B fgrep
63 are available.
64 .B egrep
65 is the same as
66 .BR "grep\ \-E" .
67 .B fgrep
68 is the same as
69 .BR "grep\ \-F" .
70 Direct invocation as either
71 .B egrep
72 or
73 .B fgrep
74 is deprecated,
75 but is provided to allow historical applications
76 that rely on them to run unmodified.
77 .
78 .SH OPTIONS
79 .SS "Generic Program Information"
80 .TP
81 .B \-\^\-help
82 Print a usage message briefly summarizing these command-line options
83 and the bug-reporting address, then exit.
84 .TP
85 .BR \-V ", " \-\^\-version
86 Print the version number of
87 .B grep
88 to the standard output stream.
89 This version number should
90 be included in all bug reports (see below).
91 .SS "Matcher Selection"
92 .TP
93 .BR \-E ", " \-\^\-extended\-regexp
94 Interpret
95 .I PATTERN
96 as an extended regular expression (ERE, see below).
97 .RB ( \-E
98 is specified by \s-1POSIX\s0.)
99 .TP
100 .BR \-F ", " \-\^\-fixed\-strings
101 Interpret
102 .I PATTERN
103 as a list of fixed strings, separated by newlines,
104 any of which is to be matched.
105 .RB ( \-F
106 is specified by \s-1POSIX\s0.)
107 .TP
108 .BR \-G ", " \-\^\-basic\-regexp
109 Interpret
110 .I PATTERN
111 as a basic regular expression (BRE, see below).
112 This is the default.
113 .TP
114 .BR \-P ", " \-\^\-perl\-regexp
115 Interpret
116 .I PATTERN
117 as a Perl regular expression.
118 This is highly experimental and
119 .B "grep \-P"
120 may warn of unimplemented features.
121 .SS "Matching Control"
122 .TP
123 .BI \-e " PATTERN" "\fR,\fP \-\^\-regexp=" PATTERN
124 Use
125 .I PATTERN
126 as the pattern.
127 This can be used to specify multiple search patterns,
128 or to protect a pattern beginning with a hyphen
129 .RB ( \- ).
130 .RB ( \-e
131 is specified by \s-1POSIX\s0.)
132 .TP
133 .BI \-f " FILE" "\fR,\fP \-\^\-file=" FILE
134 Obtain patterns from
135 .IR FILE ,
136 one per line.
137 The empty file contains zero patterns, and therefore matches nothing.
138 .RB ( \-f
139 is specified by \s-1POSIX\s0.)
140 .TP
141 .BR \-i ", " \-\^\-ignore\-case
142 Ignore case distinctions in both the
143 .I PATTERN
144 and the input files.
145 .RB ( \-i
146 is specified by \s-1POSIX\s0.)
147 .TP
148 .BR \-v ", " \-\^\-invert\-match
149 Invert the sense of matching, to select non-matching lines.
150 .RB ( \-v
151 is specified by \s-1POSIX\s0.)
152 .TP
153 .BR \-w ", " \-\^\-word\-regexp
154 Select only those lines containing matches that form whole words.
155 The test is that the matching substring must either be at the
156 beginning of the line, or preceded by a non-word constituent
157 character.
158 Similarly, it must be either at the end of the line
159 or followed by a non-word constituent character.
160 Word-constituent characters are letters, digits, and the underscore.
161 .TP
162 .BR \-x ", " \-\^\-line\-regexp
163 Select only those matches that exactly match the whole line.
164 .RB ( \-x
165 is specified by \s-1POSIX\s0.)
166 .TP
167 .B \-y
168 Obsolete synonym for
169 .BR \-i .
170 .SS "General Output Control"
171 .TP
172 .BR \-c ", " \-\^\-count
173 Suppress normal output; instead print a count of
174 matching lines for each input file.
175 With the
176 .BR \-v ", " \-\^\-invert\-match
177 option (see below), count non-matching lines.
178 .RB ( \-c
179 is specified by \s-1POSIX\s0.)
180 .TP
181 .BR \-\^\-color [ =\fIWHEN\fP "], " \-\^\-colour [ =\fIWHEN\fP ]
182 Surround the matched (non-empty) strings, matching lines, context lines,
183 file names, line numbers, byte offsets, and separators (for fields and
184 groups of context lines) with escape sequences to display them in color
185 on the terminal.
186 The colors are defined by the environment variable
187 .BR GREP_COLORS .
188 The deprecated environment variable
189 .B GREP_COLOR
190 is still supported, but its setting does not have priority.
191 .I WHEN
192 is
193 .BR never ", " always ", or " auto .
194 .TP
195 .BR \-L ", " \-\^\-files\-without\-match
196 Suppress normal output; instead print the name
197 of each input file from which no output would
198 normally have been printed.
199 The scanning will stop on the first match.
200 .TP
201 .BR \-l ", " \-\^\-files\-with\-matches
202 Suppress normal output; instead print
203 the name of each input file from which output
204 would normally have been printed.
205 The scanning will stop on the first match.
206 .RB ( \-l
207 is specified by \s-1POSIX\s0.)
208 .TP
209 .BI \-m " NUM" "\fR,\fP \-\^\-max\-count=" NUM
210 Stop reading a file after
211 .I NUM
212 matching lines.
213 If the input is standard input from a regular file,
214 and
215 .I NUM
216 matching lines are output,
217 .B grep
218 ensures that the standard input is positioned to just after the last
219 matching line before exiting, regardless of the presence of trailing
220 context lines.
221 This enables a calling process to resume a search.
222 When
223 .B grep
224 stops after
225 .I NUM
226 matching lines, it outputs any trailing context lines.
227 When the
228 .B \-c
229 or
230 .B \-\^\-count
231 option is also used,
232 .B grep
233 does not output a count greater than
234 .IR NUM .
235 When the
236 .B \-v
237 or
238 .B \-\^\-invert\-match
239 option is also used,
240 .B grep
241 stops after outputting
242 .I NUM
243 non-matching lines.
244 .TP
245 .BR \-o ", " \-\^\-only\-matching
246 Print only the matched (non-empty) parts of a matching line,
247 with each such part on a separate output line.
248 .TP
249 .BR \-q ", " \-\^\-quiet ", " \-\^\-silent
250 Quiet; do not write anything to standard output.
251 Exit immediately with zero status if any match is found,
252 even if an error was detected.
253 Also see the
254 .B \-s
255 or
256 .B \-\^\-no\-messages
257 option.
258 .RB ( \-q
259 is specified by \s-1POSIX\s0.)
260 .TP
261 .BR \-s ", " \-\^\-no\-messages
262 Suppress error messages about nonexistent or unreadable files.
263 Portability note: unlike \s-1GNU\s0
264 .BR grep ,
265 7th Edition Unix
266 .B grep
267 did not conform to \s-1POSIX\s0, because it lacked
268 .B \-q
269 and its
270 .B \-s
271 option behaved like \s-1GNU\s0
272 .BR grep 's
273 .B \-q
274 option.
275 \s-1USG\s0-style
276 .B grep
277 also lacked
278 .B \-q
279 but its
280 .B \-s
281 option behaved like \s-1GNU\s0
282 .BR grep .
283 Portable shell scripts
284 should avoid both
285 .B \-q
286 and
287 .B \-s
288 and should redirect standard and error output to
289 .B /dev/null
290 instead.
291 .RB ( \-s
292 is specified by \s-1POSIX\s0.)
293 .SS "Output Line Prefix Control"
294 .TP
295 .BR \-b ", " \-\^\-byte\-offset
296 Print the 0-based byte offset within the input file
297 before each line of output.
298 If
299 .B \-o
300 .RB ( \-\^\-only\-matching )
301 is specified,
302 print the offset of the matching part itself.
303 .TP
304 .BR \-H ", " \-\^\-with\-filename
305 Print the file name for each match.
306 This is the default when there is more than one file to search.
307 .TP
308 .BR \-h ", " \-\^\-no\-filename
309 Suppress the prefixing of file names on output.
310 This is the default when there is only one file
311 (or only standard input) to search.
312 .TP
313 .BI \-\^\-label= LABEL
314 Display input actually coming from standard input as input coming from file
315 .IR LABEL .
316 This is especially useful when implementing tools like
317 .BR zgrep ,
318 e.g.,
319 .BR "gzip -cd foo.gz | grep --label=foo -H something" .
320 See also the
321 .B \-H
322 option.
323 .TP
324 .BR \-n ", " \-\^\-line\-number
325 Prefix each line of output with the 1-based line number
326 within its input file.
327 .RB ( \-n
328 is specified by \s-1POSIX\s0.)
329 .TP
330 .BR \-T ", " \-\^\-initial\-tab
331 Make sure that the first character of actual line content lies on a
332 tab stop, so that the alignment of tabs looks normal.
333 This is useful with options that prefix their output to the actual content:
334 .BR \-H , \-n ,
335 and
336 .BR \-b .
337 In order to improve the probability that lines
338 from a single file will all start at the same column,
339 this also causes the line number and byte offset (if present)
340 to be printed in a minimum size field width.
341 .TP
342 .BR \-u ", " \-\^\-unix\-byte\-offsets
343 Report Unix-style byte offsets.
344 This switch causes
345 .B grep
346 to report byte offsets as if the file were a Unix-style text file,
347 i.e., with CR characters stripped off.
348 This will produce results identical to running
349 .B grep
350 on a Unix machine.
351 This option has no effect unless
352 .B \-b
353 option is also used;
354 it has no effect on platforms other than \s-1MS-DOS\s0 and \s-1MS\s0-Windows.
355 .TP
356 .BR \-Z ", " \-\^\-null
357 Output a zero byte (the \s-1ASCII\s0
358 .B NUL
359 character) instead of the character that normally follows a file name.
360 For example,
361 .B "grep \-lZ"
362 outputs a zero byte after each file name instead of the usual newline.
363 This option makes the output unambiguous, even in the presence of file
364 names containing unusual characters like newlines.
365 This option can be used with commands like
366 .BR "find \-print0" ,
367 .BR "perl \-0" ,
368 .BR "sort \-z" ,
369 and
370 .B "xargs \-0"
371 to process arbitrary file names,
372 even those that contain newline characters.
373 .SS "Context Line Control"
374 .TP
375 .BI \-A " NUM" "\fR,\fP \-\^\-after\-context=" NUM
376 Print
377 .I NUM
378 lines of trailing context after matching lines.
379 Places a line containing a group separator
380 .RB ( \-\^\- )
381 between contiguous groups of matches.
382 With the
383 .B \-o
384 or
385 .B \-\^\-only\-matching
386 option, this has no effect and a warning is given.
387 .TP
388 .BI \-B " NUM" "\fR,\fP \-\^\-before\-context=" NUM
389 Print
390 .I NUM
391 lines of leading context before matching lines.
392 Places a line containing a group separator
393 .RB ( \-\^\- )
394 between contiguous groups of matches.
395 With the
396 .B \-o
397 or
398 .B \-\^\-only\-matching
399 option, this has no effect and a warning is given.
400 .TP
401 .BI \-C " NUM" "\fR,\fP \-" NUM "\fR,\fP \-\^\-context=" NUM
402 Print
403 .I NUM
404 lines of output context.
405 Places a line containing a group separator
406 .RB ( \-\^\- )
407 between contiguous groups of matches.
408 With the
409 .B \-o
410 or
411 .B \-\^\-only\-matching
412 option, this has no effect and a warning is given.
413 .SS "File and Directory Selection"
414 .TP
415 .BR \-a ", " \-\^\-text
416 Process a binary file as if it were text; this is equivalent to the
417 .B \-\^\-binary\-files=text
418 option.
419 .TP
420 .BI \-\^\-binary\-files= TYPE
421 If the first few bytes of a file indicate that the file contains binary
422 data, assume that the file is of type
423 .IR TYPE .
424 By default,
425 .I TYPE
426 is
427 .BR binary ,
428 and
429 .B grep
430 normally outputs either
431 a one-line message saying that a binary file matches, or no message if
432 there is no match.
433 If
434 .I TYPE
435 is
436 .BR without-match ,
437 .B grep
438 assumes that a binary file does not match; this is equivalent to the
439 .B \-I
440 option.
441 If
442 .I TYPE
443 is
444 .BR text ,
445 .B grep
446 processes a binary file as if it were text; this is equivalent to the
447 .B \-a
448 option.
449 .I Warning:
450 .B "grep \-\^\-binary\-files=text"
451 might output binary garbage,
452 which can have nasty side effects if the output is a terminal and if the
453 terminal driver interprets some of it as commands.
454 .TP
455 .BI \-D " ACTION" "\fR,\fP \-\^\-devices=" ACTION
456 If an input file is a device, FIFO or socket, use
457 .I ACTION
458 to process it.
459 By default,
460 .I ACTION
461 is
462 .BR read ,
463 which means that devices are read just as if they were ordinary files.
464 If
465 .I ACTION
466 is
467 .BR skip ,
468 devices are silently skipped.
469 .TP
470 .BI \-d " ACTION" "\fR,\fP \-\^\-directories=" ACTION
471 If an input file is a directory, use
472 .I ACTION
473 to process it.
474 By default,
475 .I ACTION
476 is
477 .BR read ,
478 i.e., read directories just as if they were ordinary files.
479 If
480 .I ACTION
481 is
482 .BR skip ,
483 silently skip directories.
484 If
485 .I ACTION
486 is
487 .BR recurse ,
488 read all files under each directory, recursively,
489 following symbolic links only if they are on the command line.
490 This is equivalent to the
491 .B \-r
492 option.
493 .TP
494 .BI \-\^\-exclude= GLOB
495 Skip files whose base name matches
496 .I GLOB
497 (using wildcard matching).
498 A file-name glob can use
499 .BR * ,
500 .BR ? ,
501 and
502 .BR [ ... ]
503 as wildcards, and
504 .B \e
505 to quote a wildcard or backslash character literally.
506 .TP
507 .BI \-\^\-exclude-from= FILE
508 Skip files whose base name matches any of the file-name globs read from
509 .I FILE
510 (using wildcard matching as described under
511 .BR \-\^\-exclude ).
512 .TP
513 .BI \-\^\-exclude-dir= DIR
514 Exclude directories matching the pattern
515 .I DIR
516 from recursive searches.
517 .TP
518 .BR \-I
519 Process a binary file as if it did not contain matching data; this is
520 equivalent to the
521 .B \-\^\-binary\-files=without-match
522 option.
523 .TP
524 .BI \-\^\-include= GLOB
525 Search only files whose base name matches
526 .I GLOB
527 (using wildcard matching as described under
528 .BR \-\^\-exclude ).
529 .TP
530 .BR \-r ", " \-\^\-recursive
531 Read all files under each directory, recursively,
532 following symbolic links only if they are on the command line.
533 This is equivalent to the
534 .B "\-d recurse"
535 option.
536 .BR \-R ", " \-\^\-dereference\-recursive
537 Read all files under each directory, recursively.
538 Follow all symbolic links, unlike
539 .BR \-r .
540 .SS "Other Options"
541 .TP
542 .BR \-\^\-line\-buffered
543 Use line buffering on output.
544 This can cause a performance penalty.
545 .TP
546 .B \-\^\-mmap
547 If possible, use the
548 .BR mmap (2)
549 system call to read input, instead of
550 the default
551 .BR read (2)
552 system call.
553 In some situations,
554 .B \-\^\-mmap
555 yields better performance.
556 However,
557 .B \-\^\-mmap
558 can cause undefined behavior (including core dumps)
559 if an input file shrinks while
560 .B grep
561 is operating, or if an I/O error occurs.
562 .TP
563 .BR \-U ", " \-\^\-binary
564 Treat the file(s) as binary.
565 By default, under \s-1MS-DOS\s0 and \s-1MS\s0-Windows,
566 .BR grep
567 guesses the file type by looking at the contents of the first 32KB
568 read from the file.
569 If
570 .BR grep
571 decides the file is a text file, it strips the CR characters from the
572 original file contents (to make regular expressions with
573 .B ^
574 and
575 .B $
576 work correctly).
577 Specifying
578 .B \-U
579 overrules this guesswork, causing all files to be read and passed to the
580 matching mechanism verbatim; if the file is a text file with CR/LF
581 pairs at the end of each line, this will cause some regular
582 expressions to fail.
583 This option has no effect on platforms
584 other than \s-1MS-DOS\s0 and \s-1MS\s0-Windows.
585 .TP
586 .BR \-z ", " \-\^\-null\-data
587 Treat the input as a set of lines,
588 each terminated by a zero byte (the \s-1ASCII\s0
589 .B NUL
590 character) instead of a newline.
591 Like the
592 .B -Z
593 or
594 .B \-\^\-null
595 option, this option can be used with commands like
596 .B sort -z
597 to process arbitrary file names.
598 .
599 .SH "REGULAR EXPRESSIONS"
600 A regular expression is a pattern that describes a set of strings.
601 Regular expressions are constructed analogously to arithmetic
602 expressions, by using various operators to combine smaller expressions.
603 .PP
604 .B grep
605 understands three different versions of regular expression syntax:
606 \*(lqbasic,\*(rq \*(lqextended\*(rq and \*(lqperl.\*(rq In
607 .RB "\s-1GNU\s0\ " grep ,
608 there is no difference in available functionality between basic and
609 extended syntaxes.
610 In other implementations, basic regular expressions are less powerful.
611 The following description applies to extended regular expressions;
612 differences for basic regular expressions are summarized afterwards.
613 Perl regular expressions give additional functionality, and are
614 documented in pcresyntax(3) and pcrepattern(3), but may not be
615 available on every system.
616 .PP
617 The fundamental building blocks are the regular expressions
618 that match a single character.
619 Most characters, including all letters and digits,
620 are regular expressions that match themselves.
621 Any meta-character with special meaning
622 may be quoted by preceding it with a backslash.
623 .PP
624 The period
625 .B .\&
626 matches any single character.
627 .SS "Character Classes and Bracket Expressions"
628 A
629 .I "bracket expression"
630 is a list of characters enclosed by
631 .B [
632 and
633 .BR ] .
634 It matches any single
635 character in that list; if the first character of the list
636 is the caret
637 .B ^
638 then it matches any character
639 .I not
640 in the list.
641 For example, the regular expression
642 .B [0123456789]
643 matches any single digit.
644 .PP
645 Within a bracket expression, a
646 .I "range expression"
647 consists of two characters separated by a hyphen.
648 It matches any single character that sorts between the two characters,
649 inclusive, using the locale's collating sequence and character set.
650 For example, in the default C locale,
651 .B [a\-d]
652 is equivalent to
653 .BR [abcd] .
654 Many locales sort characters in dictionary order, and in these locales
655 .B [a\-d]
656 is typically not equivalent to
657 .BR [abcd] ;
658 it might be equivalent to
659 .BR [aBbCcDd] ,
660 for example.
661 To obtain the traditional interpretation of bracket expressions,
662 you can use the C locale by setting the
663 .B LC_ALL
664 environment variable to the value
665 .BR C .
666 .PP
667 Finally, certain named classes of characters are predefined within
668 bracket expressions, as follows.
669 Their names are self explanatory, and they are
670 .BR [:alnum:] ,
671 .BR [:alpha:] ,
672 .BR [:cntrl:] ,
673 .BR [:digit:] ,
674 .BR [:graph:] ,
675 .BR [:lower:] ,
676 .BR [:print:] ,
677 .BR [:punct:] ,
678 .BR [:space:] ,
679 .BR [:upper:] ,
680 and
681 .BR [:xdigit:].
682 For example,
683 .B [[:alnum:]]
684 means the character class of numbers and
685 letters in the current locale. In the C locale and \s-1ASCII\s0
686 character set encoding, this is the same as
687 .BR [0\-9A\-Za\-z] .
688 (Note that the brackets in these class names are part of the symbolic
689 names, and must be included in addition to the brackets delimiting
690 the bracket expression.)
691 Most meta-characters lose their special meaning inside bracket expressions.
692 To include a literal
693 .B ]
694 place it first in the list.
695 Similarly, to include a literal
696 .B ^
697 place it anywhere but first.
698 Finally, to include a literal
699 .B \-
700 place it last.
701 .SS Anchoring
702 The caret
703 .B ^
704 and the dollar sign
705 .B $
706 are meta-characters that respectively match the empty string at the
707 beginning and end of a line.
708 .SS "The Backslash Character and Special Expressions"
709 The symbols
710 .B \e<
711 and
712 .B \e>
713 respectively match the empty string at the beginning and end of a word.
714 The symbol
715 .B \eb
716 matches the empty string at the edge of a word,
717 and
718 .B \eB
719 matches the empty string provided it's
720 .I not
721 at the edge of a word.
722 The symbol
723 .B \ew
724 is a synonym for
725 .B [_[:alnum:]]
726 and
727 .B \eW
728 is a synonym for
729 .BR [^_[:alnum:]] .
730 .SS Repetition
731 A regular expression may be followed by one of several repetition operators:
732 .PD 0
733 .TP
734 .B ?
735 The preceding item is optional and matched at most once.
736 .TP
737 .B *
738 The preceding item will be matched zero or more times.
739 .TP
740 .B +
741 The preceding item will be matched one or more times.
742 .TP
743 .BI { n }
744 The preceding item is matched exactly
745 .I n
746 times.
747 .TP
748 .BI { n ,}
749 The preceding item is matched
750 .I n
751 or more times.
752 .TP
753 .BI { n , m }
754 The preceding item is matched at least
755 .I n
756 times, but not more than
757 .I m
758 times.
759 .PD
760 .SS Concatenation
761 Two regular expressions may be concatenated; the resulting
762 regular expression matches any string formed by concatenating
763 two substrings that respectively match the concatenated
764 expressions.
765 .SS Alternation
766 Two regular expressions may be joined by the infix operator
767 .BR | ;
768 the resulting regular expression matches any string matching
769 either alternate expression.
770 .SS Precedence
771 Repetition takes precedence over concatenation, which in turn
772 takes precedence over alternation.
773 A whole expression may be enclosed in parentheses
774 to override these precedence rules and form a subexpression.
775 .SS "Back References and Subexpressions"
776 The back-reference
777 .BI \e n\c
778 \&, where
779 .I n
780 is a single digit, matches the substring
781 previously matched by the
782 .IR n th
783 parenthesized subexpression of the regular expression.
784 .SS "Basic vs Extended Regular Expressions"
785 In basic regular expressions the meta-characters
786 .BR ? ,
787 .BR + ,
788 .BR { ,
789 .BR | ,
790 .BR ( ,
791 and
792 .BR )
793 lose their special meaning; instead use the backslashed
794 versions
795 .BR \e? ,
796 .BR \e+ ,
797 .BR \e{ ,
798 .BR \e| ,
799 .BR \e( ,
800 and
801 .BR \e) .
802 .PP
803 Traditional
804 .B egrep
805 did not support the
806 .B {
807 meta-character, and some
808 .B egrep
809 implementations support
810 .B \e{
811 instead, so portable scripts should avoid
812 .B {
813 in
814 .B "grep\ \-E"
815 patterns and should use
816 .B [{]
817 to match a literal
818 .BR { .
819 .PP
820 \s-1GNU\s0
821 .B "grep\ \-E"
822 attempts to support traditional usage by assuming that
823 .B {
824 is not special if it would be the start of an invalid interval
825 specification.
826 For example, the command
827 .B "grep\ \-E\ '{1'"
828 searches for the two-character string
829 .B {1
830 instead of reporting a syntax error in the regular expression.
831 \s-1POSIX.2\s0 allows this behavior as an extension, but portable scripts
832 should avoid it.
833 .
834 .SH "ENVIRONMENT VARIABLES"
835 The behavior of
836 .B grep
837 is affected by the following environment variables.
838 .PP
839 The locale for category
840 .BI LC_ foo
841 is specified by examining the three environment variables
842 .BR LC_ALL ,
843 .BR LC_\fIfoo\fP ,
844 .BR LANG ,
845 in that order.
846 The first of these variables that is set specifies the locale.
847 For example, if
848 .B LC_ALL
849 is not set, but
850 .B LC_MESSAGES
851 is set to
852 .BR pt_BR ,
853 then the Brazilian Portuguese locale is used for the
854 .B LC_MESSAGES
855 category.
856 The C locale is used if none of these environment variables are set,
857 if the locale catalog is not installed, or if
858 .B grep
859 was not compiled with national language support (\s-1NLS\s0).
860 .TP
861 .B GREP_OPTIONS
862 This variable specifies default options
863 to be placed in front of any explicit options.
864 For example, if
865 .B GREP_OPTIONS
866 is
867 .BR "'\-\^\-binary-files=without-match \-\^\-directories=skip'" ,
868 .B grep
869 behaves as if the two options
870 .B \-\^\-binary\-files=without-match
871 and
872 .B \-\^\-directories=skip
873 had been specified before any explicit options.
874 Option specifications are separated by whitespace.
875 A backslash escapes the next character,
876 so it can be used to specify an option containing whitespace or a backslash.
877 .TP
878 .B GREP_COLOR
879 This variable specifies the color used to highlight matched (non-empty) text.
880 It is deprecated in favor of
881 .BR GREP_COLORS ,
882 but still supported.
883 The
884 .BR mt ,
885 .BR ms ,
886 and
887 .B mc
888 capabilities of
889 .B GREP_COLORS
890 have priority over it.
891 It can only specify the color used to highlight
892 the matching non-empty text in any matching line
893 (a selected line when the
894 .B -v
895 command-line option is omitted,
896 or a context line when
897 .B -v
898 is specified).
899 The default is
900 .BR 01;31 ,
901 which means a bold red foreground text on the terminal's default background.
902 .TP
903 .B GREP_COLORS
904 Specifies the colors and other attributes
905 used to highlight various parts of the output.
906 Its value is a colon-separated list of capabilities
907 that defaults to
908 .B ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36
909 with the
910 .B rv
911 and
912 .B ne
913 boolean capabilities omitted (i.e., false).
914 Supported capabilities are as follows.
915 .RS
916 .TP
917 .B sl=
918 SGR substring for whole selected lines
919 (i.e.,
920 matching lines when the
921 .B \-v
922 command-line option is omitted,
923 or non-matching lines when
924 .B \-v
925 is specified).
926 If however the boolean
927 .B rv
928 capability
929 and the
930 .B \-v
931 command-line option are both specified,
932 it applies to context matching lines instead.
933 The default is empty (i.e., the terminal's default color pair).
934 .TP
935 .B cx=
936 SGR substring for whole context lines
937 (i.e.,
938 non-matching lines when the
939 .B \-v
940 command-line option is omitted,
941 or matching lines when
942 .B \-v
943 is specified).
944 If however the boolean
945 .B rv
946 capability
947 and the
948 .B \-v
949 command-line option are both specified,
950 it applies to selected non-matching lines instead.
951 The default is empty (i.e., the terminal's default color pair).
952 .TP
953 .B rv
954 Boolean value that reverses (swaps) the meanings of
955 the
956 .B sl=
957 and
958 .B cx=
959 capabilities
960 when the
961 .B \-v
962 command-line option is specified.
963 The default is false (i.e., the capability is omitted).
964 .TP
965 .B mt=01;31
966 SGR substring for matching non-empty text in any matching line
967 (i.e.,
968 a selected line when the
969 .B \-v
970 command-line option is omitted,
971 or a context line when
972 .B \-v
973 is specified).
974 Setting this is equivalent to setting both
975 .B ms=
976 and
977 .B mc=
978 at once to the same value.
979 The default is a bold red text foreground over the current line background.
980 .TP
981 .B ms=01;31
982 SGR substring for matching non-empty text in a selected line.
983 (This is only used when the
984 .B \-v
985 command-line option is omitted.)
986 The effect of the
987 .B sl=
988 (or
989 .B cx=
990 if
991 .BR rv )
992 capability remains active when this kicks in.
993 The default is a bold red text foreground over the current line background.
994 .TP
995 .B mc=01;31
996 SGR substring for matching non-empty text in a context line.
997 (This is only used when the
998 .B \-v
999 command-line option is specified.)
1000 The effect of the
1001 .B cx=
1002 (or
1003 .B sl=
1004 if
1005 .BR rv )
1006 capability remains active when this kicks in.
1007 The default is a bold red text foreground over the current line background.
1008 .TP
1009 .B fn=35
1010 SGR substring for file names prefixing any content line.
1011 The default is a magenta text foreground over the terminal's default background.
1012 .TP
1013 .B ln=32
1014 SGR substring for line numbers prefixing any content line.
1015 The default is a green text foreground over the terminal's default background.
1016 .TP
1017 .B bn=32
1018 SGR substring for byte offsets prefixing any content line.
1019 The default is a green text foreground over the terminal's default background.
1020 .TP
1021 .B se=36
1022 SGR substring for separators that are inserted
1023 between selected line fields
1024 .RB ( : ),
1025 between context line fields,
1026 .RB ( \- ),
1027 and between groups of adjacent lines when nonzero context is specified
1028 .RB ( \-\^\- ).
1029 The default is a cyan text foreground over the terminal's default background.
1030 .TP
1031 .B ne
1032 Boolean value that prevents clearing to the end of line
1033 using Erase in Line (EL) to Right
1034 .RB ( \\\\\\33[K )
1035 each time a colorized item ends.
1036 This is needed on terminals on which EL is not supported.
1037 It is otherwise useful on terminals
1038 for which the
1039 .B back_color_erase
1040 .RB ( bce )
1041 boolean terminfo capability does not apply,
1042 when the chosen highlight colors do not affect the background,
1043 or when EL is too slow or causes too much flicker.
1044 The default is false (i.e., the capability is omitted).
1045 .PP
1046 Note that boolean capabilities have no
1047 .BR = ...
1048 part.
1049 They are omitted (i.e., false) by default and become true when specified.
1050 .PP
1051 See the Select Graphic Rendition (SGR) section
1052 in the documentation of the text terminal that is used
1053 for permitted values and their meaning as character attributes.
1054 These substring values are integers in decimal representation
1055 and can be concatenated with semicolons.
1056 .B grep
1057 takes care of assembling the result
1058 into a complete SGR sequence
1059 .RB ( \\\\\\33[ ... m ).
1060 Common values to concatenate include
1061 .B 1
1062 for bold,
1063 .B 4
1064 for underline,
1065 .B 5
1066 for blink,
1067 .B 7
1068 for inverse,
1069 .B 39
1070 for default foreground color,
1071 .B 30
1072 to
1073 .B 37
1074 for foreground colors,
1075 .B 90
1076 to
1077 .B 97
1078 for 16-color mode foreground colors,
1079 .B 38;5;0
1080 to
1081 .B 38;5;255
1082 for 88-color and 256-color modes foreground colors,
1083 .B 49
1084 for default background color,
1085 .B 40
1086 to
1087 .B 47
1088 for background colors,
1089 .B 100
1090 to
1091 .B 107
1092 for 16-color mode background colors, and
1093 .B 48;5;0
1094 to
1095 .B 48;5;255
1096 for 88-color and 256-color modes background colors.
1097 .RE
1098 .TP
1099 \fBLC_ALL\fP, \fBLC_COLLATE\fP, \fBLANG\fP
1100 These variables specify the locale for the
1101 .B LC_COLLATE
1102 category,
1103 which determines the collating sequence
1104 used to interpret range expressions like
1105 .BR [a\-z] .
1106 .TP
1107 \fBLC_ALL\fP, \fBLC_CTYPE\fP, \fBLANG\fP
1108 These variables specify the locale for the
1109 .B LC_CTYPE
1110 category,
1111 which determines the type of characters,
1112 e.g., which characters are whitespace.
1113 .TP
1114 \fBLC_ALL\fP, \fBLC_MESSAGES\fP, \fBLANG\fP
1115 These variables specify the locale for the
1116 .B LC_MESSAGES
1117 category,
1118 which determines the language that
1119 .B grep
1120 uses for messages.
1121 The default C locale uses American English messages.
1122 .TP
1123 .B POSIXLY_CORRECT
1124 If set,
1125 .B grep
1126 behaves as \s-1POSIX.2\s0 requires; otherwise,
1127 .B grep
1128 behaves more like other \s-1GNU\s0 programs.
1129 \s-1POSIX.2\s0 requires that options that follow file names must be
1130 treated as file names; by default, such options are permuted to the
1131 front of the operand list and are treated as options.
1132 Also, \s-1POSIX.2\s0 requires that unrecognized options be diagnosed as
1133 \*(lqillegal\*(rq, but since they are not really against the law the default
1134 is to diagnose them as \*(lqinvalid\*(rq.
1135 .B POSIXLY_CORRECT
1136 also disables \fB_\fP\fIN\fP\fB_GNU_nonoption_argv_flags_\fP,
1137 described below.
1138 .TP
1139 \fB_\fP\fIN\fP\fB_GNU_nonoption_argv_flags_\fP
1140 (Here
1141 .I N
1142 is
1143 .BR grep 's
1144 numeric process ID.)  If the
1145 .IR i th
1146 character of this environment variable's value is
1147 .BR 1 ,
1148 do not consider the
1149 .IR i th
1150 operand of
1151 .B grep
1152 to be an option, even if it appears to be one.
1153 A shell can put this variable in the environment for each command it runs,
1154 specifying which operands are the results of file name wildcard
1155 expansion and therefore should not be treated as options.
1156 This behavior is available only with the \s-1GNU\s0 C library, and only
1157 when
1158 .B POSIXLY_CORRECT
1159 is not set.
1160 .
1161 .SH "EXIT STATUS"
1162 Normally, the exit status is 0 if selected lines are found and 1 otherwise.
1163 But the exit status is 2 if an error occurred, unless the
1164 .B \-q
1165 or
1166 .B \-\^\-quiet
1167 or
1168 .B \-\^\-silent
1169 option is used and a selected line is found.
1170 Note, however, that \s-1POSIX\s0 only mandates, for programs such as
1171 .BR grep ,
1172 .BR cmp ,
1173 and
1174 .BR diff ,
1175 that the exit status in case of error be greater than 1;
1176 it is therefore advisable, for the sake of portability,
1177 to use logic that tests for this general condition
1178 instead of strict equality with\ 2.
1179 .
1180 .SH COPYRIGHT
1181 Copyright 1998-2000, 2002, 2005-2012 Free Software Foundation, Inc.
1182 .PP
1183 This is free software;
1184 see the source for copying conditions.
1185 There is NO warranty;
1186 not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1187 .
1188 .SH BUGS
1189 .SS "Reporting Bugs"
1190 Email bug reports to
1191 .RB < bug\-grep@gnu.org >,
1192 a mailing list whose web page is
1193 .RB < http://lists.gnu.org/mailman/listinfo/bug\-grep >.
1194 .BR grep 's
1195 Savannah bug tracker is located at
1196 .RB < http://savannah.gnu.org/bugs/?group=grep >.
1197 .SS "Known Bugs"
1198 Large repetition counts in the
1199 .BI { n , m }
1200 construct may cause
1201 .B grep
1202 to use lots of memory.
1203 In addition,
1204 certain other obscure regular expressions require exponential time
1205 and space, and may cause
1206 .B grep
1207 to run out of memory.
1208 .PP
1209 Back-references are very slow, and may require exponential time.
1210 .
1211 .SH "SEE ALSO"
1212 .SS "Regular Manual Pages"
1213 awk(1), cmp(1), diff(1), find(1), gzip(1),
1214 perl(1), sed(1), sort(1), xargs(1), zgrep(1),
1215 mmap(2), read(2),
1216 pcre(3), pcresyntax(3), pcrepattern(3),
1217 terminfo(5),
1218 glob(7), regex(7).
1219 .SS "\s-1POSIX\s0 Programmer's Manual Page"
1220 grep(1p).
1221 .SS "\*(Txinfo Documentation"
1222 The full documentation for
1223 .B grep
1224 is maintained as a \*(Txinfo manual,
1225 which you can read at http://www.gnu.org/software/grep/manual/.
1226 If the
1227 .B info
1228 and
1229 .B grep
1230 programs are properly installed at your site, the command
1231 .IP
1232 .B info grep
1233 .PP
1234 should give you access to the complete manual.
1235 .
1236 .SH NOTES
1237 This man page is maintained only fitfully;
1238 the full documentation is often more up-to-date.
1239 .PP
1240 \s-1GNU\s0's not Unix, but Unix is a beast;
1241 its plural form is Unixen.
1242 .\" Work around problems with some troff -man implementations.
1243 .br