1 .\" Copyright (c) 1998 Andries Brouwer
3 .\" This is free documentation; you can redistribute it and/or
4 .\" modify it under the terms of the GNU General Public License as
5 .\" published by the Free Software Foundation; either version 2 of
6 .\" the License, or (at your option) any later version.
8 .\" The GNU General Public License's references to "object code"
9 .\" and "executables" are to be interpreted as the output of any
10 .\" document formatting or typesetting system, including
11 .\" intermediate and printed output.
13 .\" This manual is distributed in the hope that it will be useful,
14 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
15 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 .\" GNU General Public License for more details.
18 .\" You should have received a copy of the GNU General Public
19 .\" License along with this manual; if not, write to the Free
20 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
23 .\" 2003-08-24 fix for / by John Kristoff + joey
25 .TH GLOB 7 2003-08-24 "Linux" "Linux Programmer's Manual"
27 glob \- Globbing pathnames
29 Long ago, in UNIX V6, there was a program
31 that would expand wildcard patterns.
32 Soon afterward this became a shell built-in.
34 These days there is also a library routine
36 that will perform this function for a user program.
38 The rules are as follows (POSIX.2, 3.13).
39 .SS "Wildcard Matching"
40 A string is a wildcard pattern if it contains one of the
41 characters \(aq?\(aq, \(aq*\(aq or \(aq[\(aq.
42 Globbing is the operation
43 that expands a wildcard pattern into the list of pathnames
45 Matching is defined by:
47 A \(aq?\(aq (not between brackets) matches any single character.
49 A \(aq*\(aq (not between brackets) matches any string,
50 including the empty string.
52 .B "Character classes"
54 An expression "\fI[...]\fP" where the first character after the
55 leading \(aq[\(aq is not an \(aq!\(aq matches a single character,
56 namely any of the characters enclosed by the brackets.
57 The string enclosed by the brackets cannot be empty;
58 therefore \(aq]\(aq can be allowed between the brackets, provided
59 that it is the first character.
60 (Thus, "\fI[][!]\fP" matches the
61 three characters \(aq[\(aq, \(aq]\(aq and \(aq!\(aq.)
65 There is one special convention:
66 two characters separated by \(aq\-\(aq denote a range.
67 (Thus, "\fI[A\-Fa\-f0\-9]\fP"
68 is equivalent to "\fI[ABCDEFabcdef0123456789]\fP".)
69 One may include \(aq\-\(aq in its literal meaning by making it the
70 first or last character between the brackets.
71 (Thus, "\fI[]\-]\fP" matches just the two characters \(aq]\(aq and \(aq\-\(aq,
72 and "\fI[\-\-0]\fP" matches the
73 three characters \(aq\-\(aq, \(aq.\(aq, \(aq0\(aq, since \(aq/\(aq
78 An expression "\fI[!...]\fP" matches a single character, namely
79 any character that is not matched by the expression obtained
80 by removing the first \(aq!\(aq from it.
81 (Thus, "\fI[!]a\-]\fP" matches any
82 single character except \(aq]\(aq, \(aqa\(aq and \(aq\-\(aq.)
84 One can remove the special meaning of \(aq?\(aq, \(aq*\(aq and \(aq[\(aq by
85 preceding them by a backslash, or, in case this is part of
86 a shell command line, enclosing them in quotes.
87 Between brackets these characters stand for themselves.
88 Thus, "\fI[[?*\e]\fP" matches the
89 four characters \(aq[\(aq, \(aq?\(aq, \(aq*\(aq and \(aq\e\(aq.
91 Globbing is applied on each of the components of a pathname
93 A \(aq/\(aq in a pathname cannot be matched by a \(aq?\(aq or \(aq*\(aq
94 wildcard, or by a range like "\fI[.\-0]\fP".
95 A range cannot contain an
96 explicit \(aq/\(aq character; this would lead to a syntax error.
98 If a filename starts with a \(aq.\(aq,
99 this character must be matched explicitly.
100 (Thus, \fIrm\ *\fP will not remove .profile, and \fItar\ c\ *\fP will not
101 archive all your files; \fItar\ c\ .\fP is better.)
103 The nice and simple rule given above: "expand a wildcard pattern
104 into the list of matching pathnames" was the original UNIX
106 It allowed one to have patterns that expand into
110 xv \-wait 0 *.gif *.jpg
112 where perhaps no *.gif files are present (and this is not
114 However, POSIX requires that a wildcard pattern is left
115 unchanged when it is syntactically incorrect, or the list of
116 matching pathnames is empty.
119 one can force the classical behavior by setting
120 .IR allow_null_glob_expansion=true .
122 (Similar problems occur elsewhere.
123 E.g., where old scripts have
126 rm \`find . \-name "*~"\`
131 rm \-f nosuchfile \`find . \-name "*~"\`
133 to avoid error messages from
135 called with an empty argument list.)
137 .SS Regular expressions
138 Note that wildcard patterns are not regular expressions,
139 although they are a bit similar.
140 First of all, they match
141 filenames, rather than text, and secondly, the conventions
142 are not the same: for example, in a regular expression \(aq*\(aq means zero or
143 more copies of the preceding thing.
145 Now that regular expressions have bracket expressions where
146 the negation is indicated by a \(aq^\(aq, POSIX has declared the
147 effect of a wildcard pattern "\fI[^...]\fP" to be undefined.
148 .SS Character classes and Internationalization
149 Of course ranges were originally meant to be ASCII ranges,
150 so that "\fI[\ \-%]\fP" stands for "\fI[\ !"#$%]\fP" and "\fI[a\-z]\fP" stands
151 for "any lowercase letter".
152 Some UNIX implementations generalized this so that a range X\-Y
153 stands for the set of characters with code between the codes for
155 However, this requires the user to know the
156 character coding in use on the local system, and moreover, is
157 not convenient if the collating sequence for the local alphabet
158 differs from the ordering of the character codes.
159 Therefore, POSIX extended the bracket notation greatly,
160 both for wildcard patterns and for regular expressions.
161 In the above we saw three types of items that can occur in a bracket
162 expression: namely (i) the negation, (ii) explicit single characters,
164 POSIX specifies ranges in an internationally
165 more useful way and adds three more types:
167 (iii) Ranges X\-Y comprise all characters that fall between X
168 and Y (inclusive) in the current collating sequence as defined
171 category in the current locale.
173 (iv) Named character classes, like
176 [:alnum:] [:alpha:] [:blank:] [:cntrl:]
177 [:digit:] [:graph:] [:lower:] [:print:]
178 [:punct:] [:space:] [:upper:] [:xdigit:]
181 so that one can say "\fI[[:lower:]]\fP" instead of "\fI[a\-z]\fP", and have
182 things work in Denmark, too, where there are three letters past \(aqz\(aq
184 These character classes are defined by the
187 in the current locale.
189 (v) Collating symbols, like "\fI[.ch.]\fP" or "\fI[.a-acute.]\fP",
190 where the string between "\fI[.\fP" and "\fI.]\fP" is a collating
191 element defined for the current locale.
193 be a multicharacter element.
195 (vi) Equivalence class expressions, like "\fI[=a=]\fP",
196 where the string between "\fI[=\fP" and "\fI=]\fP" is any collating
197 element from its equivalence class, as defined for the
199 For example, "\fI[[=a=]]\fP" might be equivalent
200 .\" FIXME . the accented 'a' characters are not rendering properly
202 to "\fI[aáàäâ]\fP" (warning: Latin-1 here), that is,
203 to "\fI[a[.a-acute.][.a-grave.][.a-umlaut.][.a-circumflex.]]\fP".