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

2 Answers 2

$ 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

"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

  • 1
    foo?(.in).x matches foo.in.x and foo.x (if present). May 14, 2015 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.