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











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
->
-> Bash in-process regular expressions




Bash in-process regular expressions


Bash acquired in-process regular expressions in version 3.0, but I never noticed, probably because most of the machines I'm using are Bash 2.05b. As I'm not necessarily in a position to upgrade any old box I happen to be working on, I tend to stick to the stuff that will work anywhere, which often means piping out to grep for a regular expression test.

However, should you have an environment where you can depend on this feature being present, in-process regexes obviously avoid firing up another process for grep and are much more neat to write or read.

The syntax is Perlish, using "=~". You also have access to sub-matches: $BASH_REMATCH is the string matched, ${BASH_REMATCH[1]} is the first parenthesided match, and so on. So, we can do:


if [[ "$input" =~ 'foo(.*)' ]]
then
        echo $BASH_REMATCH is what I wanted   
        echo but just ${BASH_REMATCH[1]}
fi
 

and other more interesting tasks.

Set 'nocaseglob' for case insensitivity. The return value for these is 0 for a match, 1 for no match, and (how thoughtful) 2 if the expression is syntactically incorrect, so watch out for the other side of that "if" - a non-zero return might mean you screwed up your pattern.




If this page was useful to you, please help others find it:  





6 comments




More Articles by - Find me on Google+



Click here to add your comments
- no registration needed!




Mon Aug 4 05:18:06 2008: 4465   Taco
http://www.scheltema.org

When trying this, I could only get it to work without the single quotes:

 
if [[ $input =~ foo(.*) ]]
then
echo $BASH_REMATCH is what I wanted
echo but just ${BASH_REMATCH[1]}
fi




Sat Jul 18 15:57:55 2009: 6644   anonymous

gravatar
me too, single or double quotes don't work... strange syntax...



Mon Mar 29 10:54:01 2010: 8294   vayerx

gravatar


Any part of the pattern may be quoted to force it to be matched as a string.



Sun Aug 8 02:25:43 2010: 8878   VertigoRay
http://vertigion.com
gravatar


Worked for me as it was shown in the article ...
$ uname -srvo
Linux 2.6.32.8-grsec-2.1.14-modsign-xeon-64 #2 SMP Sat Mar 13 00:42:43 PST 2010 GNU/Linux

Thanks for the post. Really helped me out.



Mon Sep 27 13:17:09 2010: 9012   JonathanCross
http://JonathanCross.com
gravatar


I must use unquoted regex as well.

$ if [[ sam =~ ^bob|sam$ ]];then echo match;else echo NO match;fi
match
$ if [[ sam2 =~ ^bob|sam$ ]];then echo match;else echo NO match;fi
NO match

$ bash --version
GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.






Mon Jun 9 14:57:12 2014: Website: 12480   Rei

gravatar


Thx !

And for invert match:
if ! [[ "$input" =~ 'foo(.*)' ]]
then
echo $BASH_REMATCH is what I wanted
echo but just ${BASH_REMATCH[1]}
fi

Some !~ operator doesn't work.

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. We appreciate comments and article submissions.

Publishing your articles here

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.

pavatar.jpg

This post tagged:

       - Linux
       - Popular
       - Scripting
       - Shell
       - Unix















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide