Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer
1  
I'll make a slightly different guess, and say ls *.@(txt|doc) is probably what he wants. –  Gordon Davisson Jul 10 '11 at 17:45
    
So embarassed this is my first time seeing this used. 'Tis awesome! –  ajstein Jul 10 '11 at 19:13
    
I am embarrassed for having used this globbing many times before then forgetting how to use it enough to ask this question. –  Paul Jul 11 '11 at 7:49

You can do this without the extended globbing using brace expansion: ls *.{txt,doc}

share|improve this answer
    
Close, but not exactly. The extended globbing command will not give you an error if there are only .txt files and no .doc files. Your option will only not give you an error if both types of files, .txt and .doc, exist. –  wzzrd Jul 10 '11 at 20:41
    
True. shopt -s nullglob will take care of that error. –  glenn jackman Jul 11 '11 at 17:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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