Team LiB   Previous Section   Next Section

9.1 Filenames Versus Patterns

Metacharacters used in pattern matching are different from those used for filename expansion. When you issue a command on the command line, special characters are seen first by the shell, then by the program; therefore, unquoted metacharacters are interpreted by the shell for filename expansion. The command:

$ grep [A-Z]* chap[12]

could, for example, be interpreted by the shell as:

$ grep Array.c Bug.c Comp.c chap1 chap2

and grep would then try to find the pattern "Array.c" in files Bug.c, Comp.c, chap1, and chap2. To bypass the shell and pass the special characters to grep, use quotes:

$ grep "[A-Z]*" chap[12]

Double quotes suffice in most cases, but single quotes are the safest bet.

Note also that * and ? have subtly different meanings in pattern matching and filename expansion by the shell.

    Team LiB   Previous Section   Next Section