Sortix
Sortix Download Manual Development Source Code News Blog More
current nightly

Sortix volatile manual

This manual documents Sortix volatile, a development build that has not been officially released. You can instead view this document in the latest official manual.

GREP(1) General Commands Manual GREP(1)

NAME

grep — search text with regular expression patterns

SYNOPSIS

grep [-abcEFGhHiIlLnoqrRsTvxzZ] [-A lines-after] [-B lines-before] [-C lines-around] [-e pattern] [-f pattern-file] [-m max-count] [--binary-files=[binary|text|without-match]] [--color[=[always|auto|never]]] [--group-separator=separator] [--label=label] [--line-buffered] [--no-group-separator] [--no-ignore-case] pattern [file ...]

DESCRIPTION

grep reads lines of text from the standard input and writes the lines that match the basic regular expression pattern to the standard output. If files are specified, the input is the files read separately in sequential order. The file path can be set to ‘-’ to specify the standard input.

The basic regular expression bre(7) syntax is used for the pattern by default, but it is not actually a regular expression language. The extended regular expression ere(7) syntax is enabled with the -E option and should be used whenever possible, because it is superior, can actually be implemented efficiently, and actually is a regular expression language. Fixed literal strings can be matched using the -F option and a full line match can be done using the -x option.

Each matching input line is written as an output line with a preamble followed by the matching line. The preamble is empty, unless multiple files are specified or elements are requested by options. The preamble uses ‘:’ as a separator for matching lines and ‘-’ for non-matching lines. The preamble contains the input path (if multiple files are specified, or if -HrR but not -h), the line number (if -n), and the input file byte offset (if -b).

grep fails if the standard output is the same regular file as the input and the selected options would produce output that could be its own input.

The options are as follows:

-a
Treat all input lines as text, even if they contain binary data that is not validly encoded printable text.
-A lines-after, --after-context=lines-after
Include lines-after lines of context after each match.
-b, --byte-offset
Include the byte offset of the matching line in the output line preamble.
-B lines-before, --before-context=lines-before
Include lines-before lines of context before each match.
--binary-files=[binary|text|without-match]
Select how to handle matches inside binary data. binary outputs matches inside text only, but issues a message for matches inside binary data. text outputs matches even inside binary text and is equivalent to -a. without-match outputs matches inside text only and is equivalent to -I. The default behavior is binary.
-c, --count
Output the number of matching lines instead of which lines matched.
-C lines-around, --context=lines-around
Include lines-around lines of context before and after each match. This option is equivalent to setting both the -A and -B options.
--color[=[always|auto|never]]
Color the output using terminal escape sequences if the standard output is a terminal. The parameter can be set to always (always color the output regardless of the standard output device) , auto (color the output only if the standard output is a terminal) , never (never color the output). The parameter is optional and defaults to auto. The color setting is set to auto by default in this implementation. This implementation can be faster without colors because it does not need to process input lines multiple times to color multiple matches per line.
-e pattern
Select input lines matching this regular expression pattern. The syntax is selected per the -EFG options.

Input lines match if they match at least one pattern from the multiple -e and -f options. If -e is used, then the otherwise mandatory pattern operand does not exist.

-E, --extended-regexp
Use the POSIX Extended Regular Expression ere(7) syntax for the patterns. This option is heavily encouraged because it is actually a regular expression language, it can be implemented much more efficiently, and it has much fewer surprising special cases. This option overrides the -FG options.
-f pattern-file
Read additional patterns from the pattern-file, one pattern per line.

Input lines match if they match at least one pattern from the multiple -e and -f options. If -f is used, then the otherwise mandatory pattern operand does not exist.

-F, --fixed-strings
The pattern is a string literal and matches if it is a substring of the input lines. This option combines well with the -x option to match the entire line. This option overrides the -EG options.
-G, --basic-regexp
Use the POSIX Basic Regular Expression bre(7) syntax for the patterns. This syntax is discouraged because it is not a regular expression language. In particular, it is not required to portably support the alternation feature, and it is required to support back references (which cannot be implemented efficiently, requiring a worse implementation). It is recommended to use -E instead for non-trivial patterns.

This implementation does not support back references, but as a non-portable extension, it does allow using extended regular expression features by preceding the ‘|’, ‘+’, ‘?’, and ‘{’ characters with a backslash.

This syntax is the default syntax. This option overrides the -EF options.

--group-separator=separator
Whenever the -ABC options are used to include context lines around matching lines, separate each such non-adjacent group with this separator on its own output line. The default is ‘--’. This option overrides the --no-group-separator option.
-h, --no-filename
Never include the input path in the preamble. This option overrides -HrR.
-H, --with-filename
Always include the input path in the preamble. This option overrides -h.
-i, --ignore-case
Compare input lines against the patterns in a case insensitive manner per the locale rules. This option overrides --no-ignore-case.
-I
Never match lines inside binary data.
-l, --files-with-matches
Output only the paths of the input files that contain matching lines.
-L, --files-without-match
Output only the paths of the input files that do not contain matching lines.
--label=label
Use this label as the path of the standard input. The default label is ‘(standard input)’.
--line-buffered
Flush the standard output after each output line. This option overrides the standard behavior of only using line buffered semantics if the standard output is a terminal, otherwise fully buffered semantics are used by default, and matches will only be output if the buffer is full, or when the program terminates.
-m max-count
Read at most max-count matching lines from each input file. If max-count is at least one and any of the -ABC options are used as well, then the standard input is seeked back to the end of the last matching line, even though following context lines were output.
-n, --line-number
Include the input file line number in the preamble.
--no-group-separator
Do not separate non-adjacent groups with an output line controlled by the --group-separator option. This option overrides the --group-separator option.
--no-ignore-case
Compare input lines against the patterns in a case sensitive manner. This option overrides -i.
-o, --only-matching
Output only the exact matches inside each line. The non-matching parts of the input lines are omitted.
-q, --quiet
Do not output anything and only communicate the result via the exit status. If any matching line is found, then exit 0, even if an error occurs.
-r, --recursive
Recursively search any specified directories without following symbolic links. If no files are specified, then recursively search the working directory. This option overrides -R.
-R, --dereference-recursive
Recursively search any specified directories while following symbolic links. If no files are specified, then recursively search the working directory. This option overrides -r. This implementation will not detect infinite symbolic link loops in the filesystem.
-s, --no-messages
Do not error on input files and directories that could not be opened. All other errors remain.
-T, --initial-tab
Ensure the line data is aligned on a tab stop boundary.
-v, --invert-match
Match only lines that do not match any of the patterns.
-x, --line-regexp
Match only lines if any pattern matched the entire line, instead of the default substring logic.
-z, --null-data
Input lines are separated with a nul byte instead of the default line feed new line byte.
-Z, --null
Use a nul byte instead of ‘:’ and ‘-’ after the input file path in the preamble.

IMPLEMENTATION NOTES

This implementation is largely compatible with GNU grep, but has mild semantic differences and absent features. GNU grep is packaged and is available as ggrep(1) if needed.

The obsolescent egrep and fgrep programs are temporarily available as wrappers for the standard -E and -F options and may be removed in a future release.

This grep uses the standard library regcomp(3) backend to implement regular expressions and will have the same feature set.

This grep does not implement Perl compatible regular expressions (PCRE).

This grep cannot support zero bytes inside lines as the standard library regexec(3) does not support it and the handling of zero bytes differs slightly from the GNU grep behavior.

ENVIRONMENT

LANG
The default locale for locale variables that are unset or null.
LC_ALL
Overrides all the other locale variables if set.
LC_COLLATE
Compare the input lines to the patterns according to this locale's collating rules.

EXIT STATUS

grep will exit 0 on success if any input lines matched, exit 1 on success if no input lines matched, and exit 2 (or higher on other implementations) if any error occurred.

If -q, then grep will exit 0 if any input line matched, even if an error occurred.

SEE ALSO

cat(1), expr(1), find(1), nl(1), sed(1), regcomp(3), regexec(3), bre(7), ere(7)

STANDARDS

grep is standardized in IEEE Std 1003.1-2008 (“POSIX.1”).

The -aAbBCGhHILmorRTzZ options, as well as the long options, are extensions compatible with GNU grep.

HISTORY

The grep command first appeared in Version 4 AT&T UNIX. Sortix 0.8 added a port of GNU grep. Sortix 1.1 replaced grep with this original implementation.

BUGS

Back references are not implemented in regcomp(3) and cannot be used in this grep. Back references are mathematically not part of regular languages per computer science theory and cannot be implemented with an efficient algorithm for that reason.

The non-standard extension -w for word matching is not implemented yet because it requires introducing special support in regcomp(3) in order to implement it properly in combination with every option.

The -dD options as well as the --include, --exclude, --exclude-dir, and --exclude-from options are not implemented yet.

The GREP_COLORS environment variable is not implemented yet.

November 14, 2026 Sortix 1.1.0-dev
Copyright 2011-2026 Jonas 'Sortie' Termansen and contributors.
Sortix's source code is free software under the ISC license.
#sortix on irc.sortix.org
@sortix_org