0
$ 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

I find this behavior very surprising. To me, the [0-9] glob pattern should only match a file whose name consists of a single numeric digit. But it is also sometimes matching a file named [0-9] itself.

Is this a bug with glob expansion in bash?

Can I disable this, so that [0-9] never matches [0-9] (as it shouldn't)?

Pedro A
  • 145

2 Answers2

7

You should set the failglob option with 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]

Bonus question: why did the second-to-last ls print a leading space?

Because of the new "user-friendly" default quoting style of 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]
3

From 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.

Note specifically If no matching filenames are found, and the shell option nullglob is not enabled, the word is left unchanged. That is why your initial test resulted in:

ls: cannot access '[0-9]': No such file or directory
Andy Dalton
  • 13,993
  • Interesting! After reading https://unix.stackexchange.com/a/204944/163490 to learn the difference between nullglob (mentioned by you) and failglob (mentioned by the other answer), it looks like it is better to set failglob than nullglob to get my desired behavior, do you agree? – Pedro A Sep 27 '20 at 01:46
  • Yes, I think failglob is the way to go. The man page I quoted goes on to describe that, but since @zevzek already covered that in his answer, credit for that option goes to him. – Andy Dalton Sep 27 '20 at 02:12
  • Thank you two very much!! – Pedro A Sep 27 '20 at 05:00