1 .\" Copyright (c) 1998 Andries Brouwer
3 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
4 .\" This is free documentation; you can redistribute it and/or
5 .\" modify it under the terms of the GNU General Public License as
6 .\" published by the Free Software Foundation; either version 2 of
7 .\" the License, or (at your option) any later version.
9 .\" The GNU General Public License's references to "object code"
10 .\" and "executables" are to be interpreted as the output of any
11 .\" document formatting or typesetting system, including
12 .\" intermediate and printed output.
14 .\" This manual is distributed in the hope that it will be useful,
15 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
16 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 .\" GNU General Public License for more details.
19 .\" You should have received a copy of the GNU General Public
20 .\" License along with this manual; if not, see
21 .\" <http://www.gnu.org/licenses/>.
24 .\" 2003-08-24 fix for / by John Kristoff + joey
26 .TH GLOB 7 2016-10-08 "Linux" "Linux Programmer's Manual"
28 glob \- globbing pathnames
30 Long ago, in UNIX\ V6, there was a program
32 that would expand wildcard patterns.
33 Soon afterward this became a shell built-in.
35 These days there is also a library routine
37 that will perform this function for a user program.
39 The rules are as follows (POSIX.2, 3.13).
41 A string is a wildcard pattern if it contains one of the
42 characters \(aq?\(aq, \(aq*\(aq or \(aq[\(aq.
43 Globbing is the operation
44 that expands a wildcard pattern into the list of pathnames
46 Matching is defined by:
48 A \(aq?\(aq (not between brackets) matches any single character.
50 A \(aq*\(aq (not between brackets) matches any string,
51 including the empty string.
53 .B "Character classes"
55 An expression "\fI[...]\fP" where the first character after the
56 leading \(aq[\(aq is not an \(aq!\(aq matches a single character,
57 namely any of the characters enclosed by the brackets.
58 The string enclosed by the brackets cannot be empty;
59 therefore \(aq]\(aq can be allowed between the brackets, provided
60 that it is the first character.
61 (Thus, "\fI[][!]\fP" matches the
62 three characters \(aq[\(aq, \(aq]\(aq and \(aq!\(aq.)
66 There is one special convention:
67 two characters separated by \(aq\-\(aq denote a range.
68 (Thus, "\fI[A\-Fa\-f0\-9]\fP"
69 is equivalent to "\fI[ABCDEFabcdef0123456789]\fP".)
70 One may include \(aq\-\(aq in its literal meaning by making it the
71 first or last character between the brackets.
72 (Thus, "\fI[]\-]\fP" matches just the two characters \(aq]\(aq and \(aq\-\(aq,
73 and "\fI[\-\-0]\fP" matches the
74 three characters \(aq\-\(aq, \(aq.\(aq, \(aq0\(aq, since \(aq/\(aq
79 An expression "\fI[!...]\fP" matches a single character, namely
80 any character that is not matched by the expression obtained
81 by removing the first \(aq!\(aq from it.
82 (Thus, "\fI[!]a\-]\fP" matches any
83 single character except \(aq]\(aq, \(aqa\(aq and \(aq\-\(aq.)
85 One can remove the special meaning of \(aq?\(aq, \(aq*\(aq and \(aq[\(aq by
86 preceding them by a backslash, or, in case this is part of
87 a shell command line, enclosing them in quotes.
88 Between brackets these characters stand for themselves.
89 Thus, "\fI[[?*\e]\fP" matches the
90 four characters \(aq[\(aq, \(aq?\(aq, \(aq*\(aq and \(aq\e\(aq.
92 Globbing is applied on each of the components of a pathname
94 A \(aq/\(aq in a pathname cannot be matched by a \(aq?\(aq or \(aq*\(aq
95 wildcard, or by a range like "\fI[.\-0]\fP".
96 A range containing an explicit \(aq/\(aq character is syntactically incorrect.
97 (POSIX requires that syntactically incorrect patterns are left unchanged.)
99 If a filename starts with a \(aq.\(aq,
100 this character must be matched explicitly.
101 (Thus, \fIrm\ *\fP will not remove .profile, and \fItar\ c\ *\fP will not
102 archive all your files; \fItar\ c\ .\fP is better.)
104 The nice and simple rule given above: "expand a wildcard pattern
105 into the list of matching pathnames" was the original UNIX
107 It allowed one to have patterns that expand into
111 xv \-wait 0 *.gif *.jpg
114 where perhaps no *.gif files are present (and this is not
116 However, POSIX requires that a wildcard pattern is left
117 unchanged when it is syntactically incorrect, or the list of
118 matching pathnames is empty.
121 one can force the classical behavior using this command:
124 .\" In Bash v1, by setting allow_null_glob_expansion=true
126 (Similar problems occur elsewhere.
127 For example, where old scripts have
130 rm \`find . \-name "*~"\`
136 rm \-f nosuchfile \`find . \-name "*~"\`
139 to avoid error messages from
141 called with an empty argument list.)
143 .SS Regular expressions
144 Note that wildcard patterns are not regular expressions,
145 although they are a bit similar.
146 First of all, they match
147 filenames, rather than text, and secondly, the conventions
148 are not the same: for example, in a regular expression \(aq*\(aq means zero or
149 more copies of the preceding thing.
151 Now that regular expressions have bracket expressions where
152 the negation is indicated by a \(aq^\(aq, POSIX has declared the
153 effect of a wildcard pattern "\fI[^...]\fP" to be undefined.
154 .SS Character classes and internationalization
155 Of course ranges were originally meant to be ASCII ranges,
156 so that "\fI[\ \-%]\fP" stands for "\fI[\ !"#$%]\fP" and "\fI[a\-z]\fP" stands
157 for "any lowercase letter".
158 Some UNIX implementations generalized this so that a range X\-Y
159 stands for the set of characters with code between the codes for
161 However, this requires the user to know the
162 character coding in use on the local system, and moreover, is
163 not convenient if the collating sequence for the local alphabet
164 differs from the ordering of the character codes.
165 Therefore, POSIX extended the bracket notation greatly,
166 both for wildcard patterns and for regular expressions.
167 In the above we saw three types of items that can occur in a bracket
168 expression: namely (i) the negation, (ii) explicit single characters,
170 POSIX specifies ranges in an internationally
171 more useful way and adds three more types:
173 (iii) Ranges X\-Y comprise all characters that fall between X
174 and Y (inclusive) in the current collating sequence as defined
177 category in the current locale.
179 (iv) Named character classes, like
182 [:alnum:] [:alpha:] [:blank:] [:cntrl:]
183 [:digit:] [:graph:] [:lower:] [:print:]
184 [:punct:] [:space:] [:upper:] [:xdigit:]
187 so that one can say "\fI[[:lower:]]\fP" instead of "\fI[a\-z]\fP", and have
188 things work in Denmark, too, where there are three letters past \(aqz\(aq
190 These character classes are defined by the
193 in the current locale.
195 (v) Collating symbols, like "\fI[.ch.]\fP" or "\fI[.a-acute.]\fP",
196 where the string between "\fI[.\fP" and "\fI.]\fP" is a collating
197 element defined for the current locale.
199 be a multicharacter element.
201 (vi) Equivalence class expressions, like "\fI[=a=]\fP",
202 where the string between "\fI[=\fP" and "\fI=]\fP" is any collating
203 element from its equivalence class, as defined for the
205 For example, "\fI[[=a=]]\fP" might be equivalent
206 to "\fI[a\('a\(`a\(:a\(^a]\fP", that is,
207 to "\fI[a[.a-acute.][.a-grave.][.a-umlaut.][.a-circumflex.]]\fP".