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
18 A string is a wildcard pattern if it contains one of the characters
19 `?', `*' or `['. Globbing is the operation that expands a wildcard pat‐
20 tern 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
29 Character classes
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
38 Ranges
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
47 Complementation
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
61 Globbing is applied on each of the components of a pathname separately.
62 A `/' in a pathname cannot be matched by a `?' or `*' wildcard, or by a
63 range like `[.-0]'. A range cannot contain an explicit `/' character;
64 this would lead to a syntax error.
65
66 If a filename starts with a `.', this character must be matched explic‐
67 itly. (Thus, `rm *' will not remove .profile, and `tar c *' will not
68 archive all your files; `tar c .' is better.)
69
70
72 The nice and simple rule given above: `expand a wildcard pattern into
73 the list of matching pathnames' was the original Unix definition. It
74 allowed one to have patterns that expand into an empty list, as in
75 xv -wait 0 *.gif *.jpg
76 where perhaps no *.gif files are present (and this is not an error).
77 However, POSIX requires that a wildcard pattern is left unchanged when
78 it is syntactically incorrect, or the list of matching pathnames is
79 empty. With bash one can force the classical behaviour by setting
80 allow_null_glob_expansion=true.
81
82 (Similar problems occur elsewhere. E.g., where old scripts have
83 rm `find . -name "*~"`
84 new scripts require
85 rm -f nosuchfile `find . -name "*~"`
86 to avoid error messages from rm called with an empty argument list.)
87
88
90 Regular expressions
91 Note that wildcard patterns are not regular expressions, although they
92 are a bit similar. First of all, they match filenames, rather than
93 text, and secondly, the conventions are not the same: e.g., in a regu‐
94 lar expression `*' means zero or more copies of the preceding thing.
95
96 Now that regular expressions have bracket expressions where the nega‐
97 tion is indicated by a `^', POSIX has declared the effect of a wildcard
98 pattern `[^...]' to be undefined.
99
100
101 Character classes and Internationalization
102 Of course ranges were originally meant to be ASCII ranges, so that
103 `[ -%]' stands for `[ !"#$%]' and `[a-z]' stands for "any lowercase
104 letter". Some Unix implementations generalized this so that a range
105 X-Y stands for the set of characters with code between the codes for X
106 and for Y. However, this requires the user to know the character cod‐
107 ing in use on the local system, and moreover, is not convenient if the
108 collating sequence for the local alphabet differs from the ordering of
109 the character codes. Therefore, POSIX extended the bracket notation
110 greatly, both for wildcard patterns and for regular expressions. In
111 the above we saw three types of items that can occur in a bracket
112 expression: namely (i) the negation, (ii) explicit single characters,
113 and (iii) ranges. POSIX specifies ranges in an internationally more
114 useful way and adds three more types:
115
116 (iii) Ranges X-Y comprise all characters that fall between X and Y
117 (inclusive) in the current collating sequence as defined by the LC_COL‐
118 LATE category in the current locale.
119
120 (iv) Named character classes, like
121 [:alnum:] [:alpha:] [:blank:] [:cntrl:]
122 [:digit:] [:graph:] [:lower:] [:print:]
123 [:punct:] [:space:] [:upper:] [:xdigit:]
124 so that one can say `[[:lower:]]' instead of `[a-z]', and have things
125 work in Denmark, too, where there are three letters past `z' in the
126 alphabet. These character classes are defined by the LC_CTYPE category
127 in the current locale.
128
129 (v) Collating symbols, like `[.ch.]' or `[.a-acute.]', where the string
130 between `[.' and `.]' is a collating element defined for the current
131 locale. Note that this may be a multi-character element.
132
133 (vi) Equivalence class expressions, like `[=a=]', where the string
134 between `[=' and `=]' is any collating element from its equivalence
135 class, as defined for the current locale. For example, `[[=a=]]' might
136 be equivalent to `[a????]' (warning: Latin-1 here), that is, to `[a[.a-
137 acute.][.a-grave.][.a-umlaut.][.a-circumflex.]]'.
138
139
141 sh(1), fnmatch(3), glob(3), locale(7), regex(7)
142
143
144
145Unix 2003-08-24 GLOB(7)