The [ Special Characters

A pair of brackets surrounding a list of characters causes the shell to match filenames containing the individual characters. Whereas memo? matches memo followed by any character, memo[17a] is more restrictive, and matches only memol, memo7, and memoa. The brackets define a character class that includes all the characters within the brackets. (GNU calls this a character list; a GNU character class is something different.) The shell expands an argument that includes a character-class definition, by substituting each member of the character class, one at a time, in place of the brackets and their contents. The shell then passes the list of matching filenames to the program it is calling.

Each character-class definition can replace only a single character within a filename. The brackets and their contents are like a question mark that substitutes only the members of the character class.

The first of the following commands lists the names of all the files in the working directory that begin with a, e, i, o, or u. The second command displays the contents of the files named page2.txt, page4.txt, page6.txt, and page8.txt.

A hyphen within brackets defines a range of characters within a character-class definition. For example, [6-9] represents [6789], [a-z] represents all lowercase letters in English, and [a-zA-Z] represents all letters, both uppercase and lowercase, in English.

The following command lines show three ways to print the files named partO, parti, part2, part3, and part5. Each of these command lines causes the shell to call Ipr with five filenames:

$ lpr part0 parti part2 part3 part5

$ lpr part[01235]

The first command line explicitly specifies the five filenames. The second and third command lines use ambiguous file references, incorporating character-class definitions. The shell expands the argument on the second command line to include all files that have names beginning with part and ending with any of the characters in the character class. The character class is explicitly defined as 0, 1, 2, 3, and 5. The third command line also uses a character-class definition but defines the character class to be all characters in the range 0-3 plus 5.

The following command line prints 39 files, partO through part38:

The next two examples list the names of some of the files in the working directory. The first lists the files whose names start with a through m. The second lists files whose names end with x, y, or z.

optional When an exclamation point (!) or a caret (A) immediately follows the opening bracket ([) that defines a character class, the string enclosed by the brackets matches any character not between the brackets. Thus [Aab]* matches any filename that does not begin with a or b.

The following examples show that * [Aab] matches filenames that do not end with the letters a or b and that [b-d] * matches filenames that begin with b, c, or d.

$ Is *[Aab]

ac ad bc bd cc

ddcc dd

$ Is [b-d]*

ba bb bc bd cc

dd

You can match a hyphen i

(-) or a closing bracket (]) by placing it immediately before

the final closing bracket.

The next example demonstrates that the Is utility cannot interpret ambiguous file references. First Is is called with an argument of ?old. The shell expands ?old into a matching filename, hold, and passes that name to Is. The second command is the same as the first, except the ? is quoted (refer to "Special Characters" on page 146). The shell does not recognize this question mark as a special character and passes it on to Is. The Is utility generates an error message saying that it cannot find a file named ?old (because there is no file named ?old).

Is: ?o1d: No such file or directory

Like most utilities and programs, Is cannot interpret ambiguous file references; that work is left to the shell.

The shell expands ambiguous file references tip The shell does the expansion when It processes an ambiguous file reference, not the program that the shell runs. In the examples In this section, the utilities (Is, cat, echo, Ipr) never see the ambiguous file references. The shell expands the ambiguous file references and passes a list of ordinary filenames to the utility. In the previous examples, echo shows this to be true because it simply displays its arguments; it never displays the ambiguous file reference.

Was this article helpful?

0 0

Post a comment