Linux – Bash Extended Globbing gives syntax error

bashdebianlinux

Can anyone explain this:

$ bash
$ shopt -s extglob
$ ls *.(txt|doc)
bash: syntax error near unexpected token `('
$ shopt extglob
extglob         on

This is a debian squeeze install. I am expecting the extglob will interpret the brackets as the beginning of a group.

Thanks,

Paul

Best Answer

Because extglob doesn't work that way. You must put one of the modifier characters at the beginning of your pattern list ((txt|doc) in this case), as follows (from man bash):

          ?(pattern-list)
                 Matches zero or one occurrence of the given patterns
          *(pattern-list)
                 Matches zero or more occurrences of the given patterns
          +(pattern-list)
                 Matches one or more occurrences of the given patterns
          @(pattern-list)
                 Matches one of the given patterns
          !(pattern-list)
                 Matches anything except one of the given patterns

Specifically, ls *.*(txt|doc) produces the behaviour I am guessing you want.