Before we even start talking about regular expressions, a word of
caution for beginners:
regular expressions can be confusing because they look a lot like the
file matching patterns the shell uses.
Both the shell and programs that use regular expressions have special
meanings for the asterisk (*
), question mark (?
),
parentheses (()
), square brackets ([]
),
and vertical bar (|
, the "pipe").
Some of these characters even act the same way - almost.
Just remember, the shells, find, and cpio use filename matching patterns and not regular expressions.
You also have to remember that shell metacharacters are expanded before the shell passes the arguments to the program. To prevent this expansion, the special characters in a regular expression must be quoted (8.14) when passed as an argument from the shell.
The command:
$grep [A-Z]*.c chap[12]
could, for example, be interpreted by the shell as:
grep Array.c Bug.c Comp.c chap1 chap2
and so grep would then try to find the pattern "Array.c" in files Bug.c, Comp.c, chap1, and chap2.
The simplest solution in most cases is to surround the regular
expression with single quotes ('
).
-
,