「ls[0-9]」が「[0-9]」という名前のファイルを探して、悪いのは「touch 1」以降ではないのはなぜですか?

「ls[0-9]」が「[0-9]」という名前のファイルを探して、悪いのは「touch 1」以降ではないのはなぜですか?
$ mkdir temp && cd temp
$ ls [0-9]
ls: cannot access '[0-9]': No such file or directory
$ touch \[0-9\]
$ ls [0-9]
'[0-9]'
$ touch 1
$ ls
 1  '[0-9]'
$ ls [0-9]
1

私はこの振る舞いがとても驚いていると思います。私にとっては、[0-9]グローバルパターンは単一の数字で構成されるファイルとのみ一致する必要があります。しかし、これも時々[0-9]自分の名前のファイルと一致します。

これはbashのグローバル拡張バグですか?

[0-9]一致しないように[0-9](しないでください)これを無効にできますか?

答え1

failglob次のコマンドを使用してこのオプションを設定する必要がありますshopt -s failglob

$ ls [2-9]
ls: cannot access '[2-9]': No such file or directory
$ touch '[2-9]'
$ ls [2-9]
[2-9]
$ shopt -s failglob
$ ls [2-9]
bash: no match: [2-9]

追加の質問:最後に2番目のlsが先行スペースを印刷するのはなぜですか?

新しい「人間化」のため基本的な引用スタイルGNU ls:

$ touch 1
$ unset QUOTING_STYLE # use the default
$ ls
 1  '[2-9]'
$ QUOTING_STYLE=literal ls
1  [2-9]
$ ls --quoting-style=literal
1  [2-9]

答え2

からman bash

Pathname Expansion
  After  word  splitting,  unless  the -f option has been set, bash scans
  each word for the characters *, ?, and [.  If one of  these  characters
  appears,  then  the word is regarded as a pattern, and replaced with an
  alphabetically sorted list of filenames matching the pattern (see  Pat‐
  tern  Matching  below).   If  no  matching filenames are found, and the
  shell option nullglob is not enabled, the word is left  unchanged.

注意を払う一致するファイル名がなく、シェルオプション nullglob が有効になっていない場合、単語は変更されずにそのまま残ります。。これが初期テスト結果が次の理由です。

ls: cannot access '[0-9]': No such file or directory

関連情報