8

I fiddled around with extended globbing in bash, but there is some oddity with the inverse matcher !(PATTERN). Consider the following example

$ touch ab ba
$ shopt -s extglob

$ echo *a*
ab ba
$ echo !(a)*a*
ab ba
$ echo *a*!(a)
ab

Now it looks like !() matches the empty string, but why doesn't *a*!(a) match ba?

I assumed that in the second example ab is matched like so

!(a) ->  ""
*    ->  ""
a    ->  "a"
*    ->  "b"

which in turn would suggest a match of ba in the third case like so

*    ->  "b"
a    ->  "a"
*    ->  ""
!(a) ->  ""

but looks like this doesn't work.

Even considering greediness, that doesn't make any sense to me. What am I missing?

edit:
I'm on GNU bash, Version 4.1.7(2)-release (i386-apple-darwin10.4.0)

2

3 Answers 3

1

This is indeed odd. I could reproduce your behavior. The strangeness is the * behind the a character. When I execute your code:

$ echo *a*!(a)
ab

... it is as you described. But if I skip that second * it matches both files

$ echo *a!(a)
ab ba

Which means that your matching elements should be correct. The question is, why does this second asterisk not match the null string in the first example. As the documentation says it should match the null string, but it seems as if this is not working properly.

Maybe our bash versions do not match the documentation?

$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Copyright (C) 2007 Free Software Foundation, Inc.
1

This works : () added to disambiguate

echo @(*a*)!(a)
echo !(a)@(*a*)

strange : *a*!(a)c, matches abc and bac (which is normal behaviour)

0

It is a bug in Bash

Chet Ramey wrote
[...] and that will be fixed for the next version.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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