1GLOB(7) Linux Programmer's Manual GLOB(7)
2
3
4
6 glob - Globbing pathnames
7
9 Long ago, in Unix V6, there was a program /etc/glob that would expand
10 wildcard patterns. Soon afterwards this became a shell built-in.
11
12 These days there is also a library routine glob(3) that will perform
13 this function for a user program.
14
15 The rules are as follows (POSIX.2, 3.13).
16
17 Wildcard Matching
18 A string is a wildcard pattern if it contains one of the characters
19 '?', '*' or '['. Globbing is the operation that expands a wildcard
20 pattern into the list of pathnames matching the pattern. Matching is
21 defined by:
22
23 A '?' (not between brackets) matches any single character.
24
25 A '*' (not between brackets) matches any string, including the empty
26 string.
27
28 Character classes
29
30 An expression "[...]" where the first character after the leading '['
31 is not an '!' matches a single character, namely any of the characters
32 enclosed by the brackets. The string enclosed by the brackets cannot
33 be empty; therefore ']' can be allowed between the brackets, provided
34 that it is the first character. (Thus, "[][!]" matches the three char‐
35 acters '[', ']' and '!'.)
36
37 Ranges
38
39 There is one special convention: two characters separated by '-' denote
40 a range. (Thus, "[A-Fa-f0-9]" is equivalent to "[ABCDE‐
41 Fabcdef0123456789]".) One may include '-' in its literal meaning by
42 making it the first or last character between the brackets. (Thus,
43 "[]-]" matches just the two characters ']' and '-', and "[--0]" matches
44 the three characters '-', '.', '0', since '/' cannot be matched.)
45
46 Complementation
47
48 An expression "[!...]" matches a single character, namely any character
49 that is not matched by the expression obtained by removing the first
50 '!' from it. (Thus, "[!]a-]" matches any single character except ']',
51 'a' and '-'.)
52
53 One can remove the special meaning of '?', '*' and '[' by preceding
54 them by a backslash, or, in case this is part of a shell command line,
55 enclosing them in quotes. Between brackets these characters stand for
56 themselves. Thus, "[[?*\]" matches the four characters '[', '?', '*'
57 and '\'.
58
59 Pathnames
60 Globbing is applied on each of the components of a pathname separately.
61 A '/' in a pathname cannot be matched by a '?' or '*' wildcard, or by a
62 range like "[.-0]". A range cannot contain an explicit '/' character;
63 this would lead to a syntax error.
64
65 If a filename starts with a '.', this character must be matched explic‐
66 itly. (Thus, rm * will not remove .profile, and tar c * will not ar‐
67 chive all your files; tar c . is better.)
68
69 Empty Lists
70 The nice and simple rule given above: "expand a wildcard pattern into
71 the list of matching pathnames" was the original Unix definition. It
72 allowed one to have patterns that expand into an empty list, as in
73 xv -wait 0 *.gif *.jpg
74 where perhaps no *.gif files are present (and this is not an error).
75 However, POSIX requires that a wildcard pattern is left unchanged when
76 it is syntactically incorrect, or the list of matching pathnames is
77 empty. With bash one can force the classical behavior by setting
78 allow_null_glob_expansion=true.
79
80 (Similar problems occur elsewhere. E.g., where old scripts have
81 rm `find . -name "*~"`
82 new scripts require
83 rm -f nosuchfile `find . -name "*~"`
84 to avoid error messages from rm called with an empty argument list.)
85
87 Regular expressions
88 Note that wildcard patterns are not regular expressions, although they
89 are a bit similar. First of all, they match filenames, rather than
90 text, and secondly, the conventions are not the same: for example, in a
91 regular expression '*' means zero or more copies of the preceding
92 thing.
93
94 Now that regular expressions have bracket expressions where the nega‐
95 tion is indicated by a '^', POSIX has declared the effect of a wildcard
96 pattern "[^...]" to be undefined.
97
98 Character classes and Internationalization
99 Of course ranges were originally meant to be ASCII ranges, so that
100 "[ -%]" stands for "[ !"#$%]" and "[a-z]" stands for "any lowercase
101 letter". Some Unix implementations generalized this so that a range
102 X-Y stands for the set of characters with code between the codes for X
103 and for Y. However, this requires the user to know the character cod‐
104 ing in use on the local system, and moreover, is not convenient if the
105 collating sequence for the local alphabet differs from the ordering of
106 the character codes. Therefore, POSIX extended the bracket notation
107 greatly, both for wildcard patterns and for regular expressions. In
108 the above we saw three types of items that can occur in a bracket
109 expression: namely (i) the negation, (ii) explicit single characters,
110 and (iii) ranges. POSIX specifies ranges in an internationally more
111 useful way and adds three more types:
112
113 (iii) Ranges X-Y comprise all characters that fall between X and Y
114 (inclusive) in the current collating sequence as defined by the LC_COL‐
115 LATE category in the current locale.
116
117 (iv) Named character classes, like
118
119 [:alnum:] [:alpha:] [:blank:] [:cntrl:]
120 [:digit:] [:graph:] [:lower:] [:print:]
121 [:punct:] [:space:] [:upper:] [:xdigit:]
122
123 so that one can say "[[:lower:]]" instead of "[a-z]", and have things
124 work in Denmark, too, where there are three letters past 'z' in the
125 alphabet. These character classes are defined by the LC_CTYPE category
126 in the current locale.
127
128 (v) Collating symbols, like "[.ch.]" or "[.a-acute.]", where the string
129 between "[." and ".]" is a collating element defined for the current
130 locale. Note that this may be a multi-character element.
131
132 (vi) Equivalence class expressions, like "[=a=]", where the string
133 between "[=" and "=]" is any collating element from its equivalence
134 class, as defined for the current locale. For example, "[[=a=]]" might
135 be equivalent to "[a????]" (warning: Latin-1 here), that is, to "[a[.a-
136 acute.][.a-grave.][.a-umlaut.][.a-circumflex.]]".
137
139 sh(1), fnmatch(3), glob(3), locale(7), regex(7)
140
142 This page is part of release 3.22 of the Linux man-pages project. A
143 description of the project, and information about reporting bugs, can
144 be found at http://www.kernel.org/doc/man-pages/.
145
146
147
148Linux 2003-08-24 GLOB(7)