glob

Section: Environments, Tables, and Troff Macros (7)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

glob - パス名を glob する  

説明

昔々 UNIX V6 では、ワイルドカードパターンを展開する /etc/glob と言うプログラムがあった。その後すぐに、 この機能はシェルに組み込まれるようになった。

今日では、この機能をユーザープログラムからも実行できるよう、 glob(3) というライブラリルーチンも存在している。

glob の規則を以下に述べる (POSIX.2 3.13)。  

ワイルドカードマッチ

文字列に '?', '*', '[' が含まれていると、 それはワイルドカードパターンとみなされる。 「glob する」というのは、ワイルドカードパターンを展開して、 そのパターンにマッチするパス名のリストを得ることである。 マッチは以下のように定義される。

(ブラケット外部の) '?' はあらゆる単一の文字にマッチする。

(ブラケット外部の) '*' はあらゆる文字列にマッチする。 空文字列 (empty string) にもマッチする。

文字クラス (character class)

"[...]" と言う表記は、先頭の '[' に続く最初の文字が '!' で なければ、ブラケットの中に含まれている文字のどれか一つにマッチする。 ブラケットの内部に含まれる文字列は空であってはならない。 したがって ']' も最初の文字に指定すればブラケットの内部に含めることが できる (つまり"[][!]" は '[', ']', '!' の 3 文字のどれかにマッチする)。

領域指定 (range)

There is one special convention: two characters separated by '-' denote a range. (Thus, "[A-Fa-f0-9]" is equivalent to "[ABCDEFabcdef0123456789]".) One may include '-' in its literal meaning by making it the first or last character between the brackets. (Thus, "[]-]" matches just the two characters ']' and '-', and "[--0]" matches the three characters '-', '.', and '0', since '/' cannot be matched.)

補集合 (complementation)

An expression "[!...]" matches a single character, namely any character that is not matched by the expression obtained by removing the first '!' from it. (Thus, "[!]a-]" matches any single character except ']', 'a', and '-'.)

One can remove the special meaning of '?', '*', and '[' by preceding them by a backslash, or, in case this is part of a shell command line, enclosing them in quotes. Between brackets these characters stand for themselves. Thus, "[[?*\]" matches the four characters '[', '?', '*', and '\'.  

パス名 (pathname)

Globbing is applied on each of the components of a pathname separately. A '/' in a pathname cannot be matched by a '?' or '*' wildcard, or by a range like "[.-0]". A range containing an explicit '/' character is syntactically incorrect. (POSIX requires that syntactically incorrect patterns are left unchanged.)

'.' で始まるパス名では、この文字は陽にマッチさせなければならない。 (つまり rm * は .profile を削除しない。また tar c * ではすべてのファイルはアーカイブされない。 tar c . の方が良い。)  

空のリスト

先に与えた、わかりやすく簡単なルール、 「ワイルドカードパターンをマッチしたパス名のリストに展開する」と言うのは、 オリジナルの UNIX における定義であった。 これはパターンが空のリストに展開されることも許可されていた。 例えば

    xv -wait 0 *.gif *.jpg

において、*.gif ファイルが全くない場合でも、 これは空のリストに展開されるため、エラーにならない。 しかし POSIX では、文法的に正しくないパターンや、 マッチがなかったパターンは、 そのまま変更されずに残されることになっている。 bash では、次のコマンドで昔からの振る舞いに設定することができる。

shopt -s nullglob

(同様の問題は別のところでも起こっている。例えば、古いスクリプトにおける

rm `find . -name "*~"`

のような記述は、新しいスクリプトでは

rm -f nosuchfile `find . -name "*~"`

のようにしなければならない。さもないと rm を引数リストなしで呼び出す可能性があり、 エラーメッセージが出てしまう。)  

注意

 

正規表現

Note that wildcard patterns are not regular expressions, although they are a bit similar. First of all, they match filenames, rather than text, and secondly, the conventions are not the same: for example, in a regular expression '*' means zero or more copies of the preceding thing.

正規表現にもブラケット表現はあるが、否定は '^' でなされる。 POSIX ではワイルドカードパターンにおける "[^...]" を未定義であるとしている。  

文字クラスと国際化

Of course ranges were originally meant to be ASCII ranges, so that "[ -%]" stands for "[ !"#$%]" and "[a-z]" stands for "any lowercase letter". Some UNIX implementations generalized this so that a range X-Y stands for the set of characters with code between the codes for X and for Y. However, this requires the user to know the character coding in use on the local system, and moreover, is not convenient if the collating sequence for the local alphabet differs from the ordering of the character codes. Therefore, POSIX extended the bracket notation greatly, both for wildcard patterns and for regular expressions. In the above we saw three types of items that can occur in a bracket expression: namely (i) the negation, (ii) explicit single characters, and (iii) ranges. POSIX specifies ranges in an internationally more useful way and adds three more types:

(iii) 領域指定 X-Y は X と Y に挟まれた (両端含む) すべての文字を意味する。 このとき、カレントロケール (current locale) の LC_COLLATE カテゴリーで定義されている照合順序が用いられる。

(iv) 名前付き文字クラス: 以下のようなものである。

[:alnum:]  [:alpha:]  [:blank:]  [:cntrl:]
[:digit:]  [:graph:]  [:lower:]  [:print:]
[:punct:]  [:space:]  [:upper:]  [:xdigit:]

so that one can say "[[:lower:]]" instead of "[a-z]", and have things work in Denmark, too, where there are three letters past 'z' in the alphabet. These character classes are defined by the LC_CTYPE category in the current locale.

(v) Collating symbols, like "[.ch.]" or "[.a-acute.]", where the string between "[." and ".]" is a collating element defined for the current locale. Note that this may be a multicharacter element.

(vi) Equivalence class expressions, like "[=a=]", where the string between "[=" and "=]" is any collating element from its equivalence class, as defined for the current locale. For example, "[[=a=]]" might be equivalent to "[aáàäâ]", that is, to "[a[.a-acute.][.a-grave.][.a-umlaut.][.a-circumflex.]]".  

関連項目

sh(1), fnmatch(3), glob(3), locale(7), regex(7)


 

Index

名前
説明
ワイルドカードマッチ
パス名 (pathname)
空のリスト
注意
正規表現
文字クラスと国際化
関連項目

This document was created by man2html, using the manual pages.