2005/05/25 extglob (extended pattern matching bash)

Sometimes you want to match filenames but the standard shell expansion won't do it. For example, you want all the files but not the ones that end with .bak - there's no easy way to do that with standard wildcards, but bash can turn on "extended globbing". You do that with " shopt -s extglob". With that in place, you can do things like:

# everything but .html, .jpg or ,gif files
ls -d !(*.html|*gif|*jpg)
# list file9, file22 but not fileit
ls file+([0-9])
# begins with apl or un only
ls -d +(apl*|un*)

The lead-in characters recognized when extglob is set are (from the man page):

                     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 exactly one of the given patterns
                     Matches anything except one of the given patterns

Got something to add? Send me email.

(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> 2005/05/25 extglob (extended pattern matching bash)


Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Tony Lawrence

Wed May 25 05:39:22 2005: 569   Bela

These extended pattern matching facilities originated in the Korn shell a long time ago. See
(link) , (link) , (link) (search for "Shell regular expressions").

One odd thing. I had been given to understand that the Posix shell specification (as found e.g. in the Single Unix Specification v3, 2004) essentially codified Korn shell practice. (bash vaguely attempts to be compatible, but has too many odd angles to really match the spec.) Anyway, the Posix shell spec doesn't say a word about this sort of extended pattern matching. So that's at least one area where ksh functionality is omitted.


Wed May 25 05:41:01 2005: 570   anonymous

"Don't put URL links in HTML format for posts; we'll do that automatically". Yeah, sure, but it does a poor job of parsing the URLs out of surrounding context. 2 out of 3 above are spoiled by including the trailing ','.


Wed May 25 09:03:40 2005: 571   TonyLawrence

Ayup, sorry about that. My bad code - parsing this stuff isn't easy. I've fixed those above and will take another look at the code.

Wed May 25 09:40:24 2005: 573   Bela

It's because parsing it isn't easy that you _should_ allow the poster to tag URLs. Doesn't have to be formal HTML, could be a simplified <URL>xyzzy://abc.def</url> format that you upgrade in your posting parser.


Wed May 25 10:48:47 2005: 574   TonyLawrence

As usual, a good idea :-)

The comma thing at the end of a url is a little tough because there can be commas within a url, but I think it will work correctly now. I'll add a <URL> tag later.

If you do ever have trouble with url's, just leave a space where you want it to end.

Kerio Samepage

Have you tried Searching this site?

Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

The difference between e-mail and regular mail is that computers handle e-mail, and computers never decide to come to work one day and shoot all the other computers. (Jamais Cascio)

This post tagged: