home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 26.1 That's an Expression Chapter 26
Regular Expressions (Pattern Matching)
Next: 26.3 Understanding Expressions

26.2 Don't Confuse Regular Expressions with Wildcards

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 ( ' ).

- BB , DG, TOR

Previous: 26.1 That's an Expression UNIX Power Tools Next: 26.3 Understanding Expressions
26.1 That's an Expression Book Index 26.3 Understanding Expressions

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System