Search code examples
bashglob

Why doesn't this Bash globbing pattern work?


I was reading this answer on how to use negated glob patterns in Bash and I'm wondering why this glob pattern works:

$ ls !(*-renamed).jpg
IMG_1234.jpg

But this one does not:

$ ls *!(-renamed).jpg
IMG_1234.jpg         IMG_1236-renamed.jpg

Is it because * is too greedy? Or does it behave differently inside parentheses? I couldn't find an explanation in the Bash manual.


Solution

  • Because the * matches IMG_1236-renamed, !(-renamed) matches an empty string, and .jpg matches itself.