Extended Glob: What is the difference in syntax between ?(list), *(list), +(list) and @(list)

I have a question after reading about extended glob.

After using shopt -s extglob,

What is the difference in the following?

?(list): Matches zero or one occurrence of the given patterns.

*(list): Matches zero or more occurrences of the given patterns.

+(list): Matches one or more occurrences of the given patterns.

@(list): Matches one of the given patterns.

Yes, I have read the above description that accompanies them, but for practical purpose, I can’t see situations where people would prefer ?(list) over *(list). That is, I don’t see any difference.

I’ve tried the following:

$ ls
> test1.in test2.in test1.out test2.out`

$ echo *(*.in)
> test1.in test2.in

$ echo ?(*.in)
> test1.in test2.in

I’d expect $ echo ?(*.in) to output test1.in only, from the description, but it does not appear to be the case. Thus, could anyone give an example where it makes a difference regarding the type of extended glob used?

Source: http://mywiki.wooledge.org/BashGuide/Patterns#Extended_Globs

Asked By: fer

$ shopt -s extglob
$ ls
abbc  abc  ac
$ echo a*(b)c
abbc abc ac
$ echo a+(b)c
abbc abc
$ echo a?(b)c
abc ac
$ echo a@(b)c
Answered By: Stéphane Chazelas

“One or more occurrence” means one or more within the same file name, not one or more file names matching. foo?(.in).x matches foo.in.x while foo*(.in).x should also match foo.in.in.x

Answered By: Bjorn Munch
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.