]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/glob.7
Convert to American spelling conventions
[thirdparty/man-pages.git] / man7 / glob.7
CommitLineData
fea681da
MK
1.\" Copyright (c) 1998 Andries Brouwer
2.\"
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.
7.\"
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.
12.\"
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.
17.\"
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,
21.\" USA.
22.\"
23.\" 2003-08-24 fix for / by John Kristoff + joey
24.\"
69289f8a 25.TH GLOB 7 2003-08-24 "Linux" "Linux Programmer's Manual"
fea681da
MK
26.SH NAME
27glob \- Globbing pathnames
28.SH DESCRIPTION
29Long ago, in Unix V6, there was a program
30.I /etc/glob
31that would expand wildcard patterns.
32Soon afterwards this became a shell built-in.
33
34These days there is also a library routine
35.BR glob (3)
36that will perform this function for a user program.
37
4dec66f9 38The rules are as follows (POSIX.2, 3.13).
fea681da
MK
39.SH "WILDCARD MATCHING"
40A string is a wildcard pattern if it contains one of the
c13182ef
MK
41characters `?', `*' or `['.
42Globbing is the operation
fea681da 43that expands a wildcard pattern into the list of pathnames
c13182ef
MK
44matching the pattern.
45Matching is defined by:
fea681da
MK
46
47A `?' (not between brackets) matches any single character.
48
49A `*' (not between brackets) matches any string,
50including the empty string.
fea681da
MK
51.SS "Character classes"
52An expression `[...]' where the first character after the
53leading `[' is not an `!' matches a single character,
54namely any of the characters enclosed by the brackets.
55The string enclosed by the brackets cannot be empty;
56therefore `]' can be allowed between the brackets, provided
c13182ef
MK
57that it is the first character.
58(Thus, `[][!]' matches the three characters `[', `]' and `!'.)
fea681da
MK
59.SS Ranges
60There is one special convention:
4d9b6984
MK
61two characters separated by `\-' denote a range.
62(Thus, `[A\-Fa\-f0\-9]' is equivalent to `[ABCDEFabcdef0123456789]'.)
63One may include `\-' in its literal meaning by making it the
fea681da 64first or last character between the brackets.
4d9b6984
MK
65(Thus, `[]\-]' matches just the two characters `]' and `\-',
66and `[\-\-0]' matches the three characters `\-', `.', `0', since `/'
fea681da 67cannot be matched.)
fea681da
MK
68.SS Complementation
69An expression `[!...]' matches a single character, namely
70any character that is not matched by the expression obtained
71by removing the first `!' from it.
4d9b6984 72(Thus, `[!]a\-]' matches any single character except `]', `a' and `\-'.)
fea681da
MK
73
74One can remove the special meaning of `?', `*' and `[' by
75preceding them by a backslash, or, in case this is part of
76a shell command line, enclosing them in quotes.
77Between brackets these characters stand for themselves.
78Thus, `[[?*\e]' matches the four characters `[', `?', `*' and `\e'.
fea681da
MK
79.SH PATHNAMES
80Globbing is applied on each of the components of a pathname
c13182ef
MK
81separately.
82A `/' in a pathname cannot be matched by a `?' or `*'
83wildcard, or by a range like `[.\-0]'.
84A range cannot contain an
fea681da
MK
85explicit `/' character; this would lead to a syntax error.
86
87If a filename starts with a `.', this character must be matched explicitly.
88(Thus, `rm *' will not remove .profile, and `tar c *' will not
89archive all your files; `tar c .' is better.)
fea681da
MK
90.SH "EMPTY LISTS"
91The nice and simple rule given above: `expand a wildcard pattern
92into the list of matching pathnames' was the original Unix
c13182ef
MK
93definition.
94It allowed one to have patterns that expand into
fea681da
MK
95an empty list, as in
96.br
97.nf
7295b7ed 98 xv \-wait 0 *.gif *.jpg
fea681da
MK
99.fi
100where perhaps no *.gif files are present (and this is not
101an error).
102However, POSIX requires that a wildcard pattern is left
103unchanged when it is syntactically incorrect, or the list of
104matching pathnames is empty.
105With
106.I bash
d9bfdb9c 107one can force the classical behavior by setting
fea681da
MK
108.IR allow_null_glob_expansion=true .
109
c13182ef
MK
110(Similar problems occur elsewhere.
111E.g., where old scripts have
fea681da
MK
112.br
113.nf
7295b7ed 114 rm `find . \-name "*~"`
fea681da
MK
115.fi
116new scripts require
117.br
118.nf
7295b7ed 119 rm \-f nosuchfile `find . \-name "*~"`
fea681da
MK
120.fi
121to avoid error messages from
122.I rm
123called with an empty argument list.)
fea681da
MK
124.SH NOTES
125.SS Regular expressions
126Note that wildcard patterns are not regular expressions,
c13182ef
MK
127although they are a bit similar.
128First of all, they match
fea681da
MK
129filenames, rather than text, and secondly, the conventions
130are not the same: e.g., in a regular expression `*' means zero or
131more copies of the preceding thing.
132
133Now that regular expressions have bracket expressions where
134the negation is indicated by a `^', POSIX has declared the
135effect of a wildcard pattern `[^...]' to be undefined.
fea681da
MK
136.SS Character classes and Internationalization
137Of course ranges were originally meant to be ASCII ranges,
4d9b6984 138so that `[\ \-%]' stands for `[\ !"#$%]' and `[a\-z]' stands
fea681da 139for "any lowercase letter".
4d9b6984 140Some Unix implementations generalized this so that a range X\-Y
fea681da 141stands for the set of characters with code between the codes for
c13182ef
MK
142X and for Y.
143However, this requires the user to know the
fea681da
MK
144character coding in use on the local system, and moreover, is
145not convenient if the collating sequence for the local alphabet
146differs from the ordering of the character codes.
147Therefore, POSIX extended the bracket notation greatly,
148both for wildcard patterns and for regular expressions.
149In the above we saw three types of items that can occur in a bracket
150expression: namely (i) the negation, (ii) explicit single characters,
c13182ef
MK
151and (iii) ranges.
152POSIX specifies ranges in an internationally
fea681da
MK
153more useful way and adds three more types:
154
4d9b6984 155(iii) Ranges X\-Y comprise all characters that fall between X
9fdfa163 156and Y (inclusive) in the current collating sequence as defined
fea681da
MK
157by the LC_COLLATE category in the current locale.
158
159(iv) Named character classes, like
fea681da 160.nf
cf0a9ace 161
fea681da
MK
162[:alnum:] [:alpha:] [:blank:] [:cntrl:]
163[:digit:] [:graph:] [:lower:] [:print:]
164[:punct:] [:space:] [:upper:] [:xdigit:]
cf0a9ace 165
fea681da 166.fi
4d9b6984 167so that one can say `[[:lower:]]' instead of `[a\-z]', and have
fea681da
MK
168things work in Denmark, too, where there are three letters past `z'
169in the alphabet.
170These character classes are defined by the LC_CTYPE category
171in the current locale.
172
173(v) Collating symbols, like `[.ch.]' or `[.a-acute.]',
174where the string between `[.' and `.]' is a collating
c13182ef
MK
175element defined for the current locale.
176Note that this may
fea681da
MK
177be a multi-character element.
178
179(vi) Equivalence class expressions, like `[=a=]',
180where the string between `[=' and `=]' is any collating
181element from its equivalence class, as defined for the
c13182ef
MK
182current locale.
183For example, `[[=a=]]' might be equivalent
9044f1b8
MK
184.\" FIXME the accented 'a' characters are not rendering properly
185.\" mtk May 2007
fea681da
MK
186