1 .\" Copyright (c) 1998 Andries Brouwer
3 .\" SPDX-License-Identifier: GPL-2.0-or-later
5 .\" 2003-08-24 fix for / by John Kristoff + joey
7 .TH glob 7 (date) "Linux man-pages (unreleased)"
9 glob \- globbing pathnames
11 Long ago, in UNIX\ V6, there was a program
13 that would expand wildcard patterns.
14 Soon afterward this became a shell built-in.
16 These days there is also a library routine
18 that will perform this function for a user program.
20 The rules are as follows (POSIX.2, 3.13).
22 A string is a wildcard pattern if it contains one of the
23 characters \[aq]?\[aq], \[aq]*\[aq], or \[aq][\[aq].
24 Globbing is the operation
25 that expands a wildcard pattern into the list of pathnames
27 Matching is defined by:
29 A \[aq]?\[aq] (not between brackets) matches any single character.
31 A \[aq]*\[aq] (not between brackets) matches any string,
32 including the empty string.
34 .B "Character classes"
36 An expression "\fI[...]\fP" where the first character after the
37 leading \[aq][\[aq] is not an \[aq]!\[aq] matches a single character,
38 namely any of the characters enclosed by the brackets.
39 The string enclosed by the brackets cannot be empty;
40 therefore \[aq]]\[aq] can be allowed between the brackets, provided
41 that it is the first character.
42 (Thus, "\fI[][!]\fP" matches the
43 three characters \[aq][\[aq], \[aq]]\[aq], and \[aq]!\[aq].)
47 There is one special convention:
48 two characters separated by \[aq]\-\[aq] denote a range.
50 "\fI[A\-Fa\-f0\-9]\fP" is equivalent to "\fI[ABCDEFabcdef0123456789]\fP".)
51 One may include \[aq]\-\[aq] in its literal meaning
52 by making it the first or last character between the brackets.
54 "\fI[]\-]\fP" matches just the two characters \[aq]]\[aq] and \[aq]\-\[aq],
55 and "\fI[\-\-0]\fP" matches the
56 three characters \[aq]\-\[aq], \[aq].\[aq], and \[aq]0\[aq],
57 since \[aq]/\[aq] cannot be matched.)
61 An expression "\fI[!...]\fP" matches a single character, namely
62 any character that is not matched by the expression obtained
63 by removing the first \[aq]!\[aq] from it.
64 (Thus, "\fI[!]a\-]\fP" matches any
65 single character except \[aq]]\[aq], \[aq]a\[aq], and \[aq]\-\[aq].)
67 One can remove the special meaning of \[aq]?\[aq], \[aq]*\[aq], and \[aq][\[aq]
68 by preceding them by a backslash,
70 in case this is part of a shell command line,
71 enclosing them in quotes.
72 Between brackets these characters stand for themselves.
73 Thus, "\fI[[?*\[rs]]\fP" matches the
74 four characters \[aq][\[aq], \[aq]?\[aq], \[aq]*\[aq], and \[aq]\[rs]\[aq].
76 Globbing is applied on each of the components of a pathname
78 A \[aq]/\[aq] in a pathname cannot be matched by a \[aq]?\[aq] or \[aq]*\[aq]
79 wildcard, or by a range like "\fI[.\-0]\fP".
80 A range containing an explicit \[aq]/\[aq] character is syntactically incorrect.
81 (POSIX requires that syntactically incorrect patterns are left unchanged.)
83 If a filename starts with a \[aq].\[aq],
84 this character must be matched explicitly.
85 (Thus, \fIrm\ *\fP will not remove .profile, and \fItar\ c\ *\fP will not
86 archive all your files; \fItar\ c\ .\fP is better.)
88 The nice and simple rule given above: "expand a wildcard pattern
89 into the list of matching pathnames" was the original UNIX
91 It allowed one to have patterns that expand into
95 xv \-wait 0 *.gif *.jpg
98 where perhaps no *.gif files are present (and this is not
100 However, POSIX requires that a wildcard pattern is left
101 unchanged when it is syntactically incorrect, or the list of
102 matching pathnames is empty.
105 one can force the classical behavior using this command:
112 .\" In Bash v1, by setting allow_null_glob_expansion=true
114 (Similar problems occur elsewhere.
115 For example, where old scripts have
119 rm \`find . \-name "*\[ti]"\`
127 rm \-f nosuchfile \`find . \-name "*\[ti]"\`
131 to avoid error messages from
133 called with an empty argument list.)
135 .SS Regular expressions
136 Note that wildcard patterns are not regular expressions,
137 although they are a bit similar.
138 First of all, they match
139 filenames, rather than text, and secondly, the conventions
140 are not the same: for example, in a regular expression \[aq]*\[aq] means zero or
141 more copies of the preceding thing.
143 Now that regular expressions have bracket expressions where
144 the negation is indicated by a \[aq]\[ha]\[aq], POSIX has declared the
145 effect of a wildcard pattern "\fI[\[ha]...]\fP" to be undefined.
146 .SS Character classes and internationalization
147 Of course ranges were originally meant to be ASCII ranges,
148 so that "\fI[\ \-%]\fP" stands for "\fI[\ !"#$%]\fP" and "\fI[a\-z]\fP" stands
149 for "any lowercase letter".
150 Some UNIX implementations generalized this so that a range X\-Y
151 stands for the set of characters with code between the codes for
153 However, this requires the user to know the
154 character coding in use on the local system, and moreover, is
155 not convenient if the collating sequence for the local alphabet
156 differs from the ordering of the character codes.
157 Therefore, POSIX extended the bracket notation greatly,
158 both for wildcard patterns and for regular expressions.
159 In the above we saw three types of items that can occur in a bracket
160 expression: namely (i) the negation, (ii) explicit single characters,
162 POSIX specifies ranges in an internationally
163 more useful way and adds three more types:
165 (iii) Ranges X\-Y comprise all characters that fall between X
166 and Y (inclusive) in the current collating sequence as defined
169 category in the current locale.
171 (iv) Named character classes, like
174 [:alnum:] [:alpha:] [:blank:] [:cntrl:]
175 [:digit:] [:graph:] [:lower:] [:print:]
176 [:punct:] [:space:] [:upper:] [:xdigit:]
179 so that one can say "\fI[[:lower:]]\fP" instead of "\fI[a\-z]\fP", and have
180 things work in Denmark, too, where there are three letters past \[aq]z\[aq]
182 These character classes are defined by the
185 in the current locale.
187 (v) Collating symbols, like "\fI[.ch.]\fP" or "\fI[.a-acute.]\fP",
188 where the string between "\fI[.\fP" and "\fI.]\fP" is a collating
189 element defined for the current locale.
191 be a multicharacter element.
193 (vi) Equivalence class expressions, like "\fI[=a=]\fP",
194 where the string between "\fI[=\fP" and "\fI=]\fP" is any collating
195 element from its equivalence class, as defined for the
197 For example, "\fI[[=a=]]\fP" might be equivalent
198 to "\fI[a\('a\(`a\(:a\(^a]\fP", that is,
199 to "\fI[a[.a-acute.][.a-grave.][.a-umlaut.][.a-circumflex.]]\fP".