Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.



share|improve this question
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):

                 Matches zero or one occurrence of the given patterns
                 Matches zero or more occurrences of the given patterns
                 Matches one or more occurrences of the given patterns
                 Matches one of the given patterns
                 Matches anything except one of the given patterns

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

share|improve this answer
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


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.