bash 4.1.2 and "broken" regular expression matching conditional operator (=~)

Ville Skyttä ville.skytta at iki.fi
Tue Jan 26 17:22:11 UTC 2010


On Tuesday 26 January 2010, Roman Rakus wrote:
> Hi all,
> please take a look at bash faq section E - E14. Quoting:
> 
> E14) Why does quoting the pattern argument to the regular expression
>  matching conditional operator (=~) cause regexp matching to stop working?
[...]
> Please, take a look in your bash scripts and fix them.

Note also that in many cases it is possible to avoid using =~ altogether and 
use plain == and simple globbing (possibly with multiple tests), and in even 
more cases with extended globbing turned on.  In addition to fewer portability 
issues, globbing tends to be faster too.

If using extended globbing, just remember to reset extglob back to what it was 
if your script is a sourced, not executed one, e.g. something like:

    reset_extglob=$(shopt -p extglob)
    shopt -s extglob
    [[ "afoo" == *@(foo|bar|quux) ]] && echo hello
    # ...
    $reset_extglob

(We're not doing the reset in bash-completion yet, but eventually hopefully 
will, or find another way to circumvent modifying users' shopt states.)


More information about the devel mailing list