APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

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

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

Printer Friendly Version

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


More Articles by

Find me on Google+

Click here to add your comments
- no registration needed!

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.

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments

If you want a picture to show with your comment, go get a Gravatar
Kerio Samepage

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-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

Jump to Comments

Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.