15

I use RegEx Buddy to prototype and debug my regular expressions. RegEx Buddy allows me to choose between a number of different regular expression engine types (.NET, Java, Perl, GNU BRE, GNU ERE, POSIX, BRE, POSIX ERE etc).

What regular expression engine does bash use (for example in if and case statements)? I'm running Centos 5.5 32 bit and bash 3.2.25(1):

[kevin@mon01 scratch]$ bash --version
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

I'm guessing it'll be GNU BRE or GNU ERE?

1
  • zsh supports PCRE with [[ sth =~ regex ]] (enable it with setopt re_match_pcre).
    – HappyFace
    May 27, 2021 at 19:58

3 Answers 3

17

bash (and POSIX shells in general) do not use regular expressions in the case statement, rather glob patterns.

There's limited support for regular expressions using the =~ operator; see details at: http://mywiki.wooledge.org/BashGuide/Patterns,
which says that bash uses Extended Regular Expressions (ERE).

1
  • Thanks. Second link mentions ERE which I am guessing is GNU ERE.
    – Kev
    Jun 23, 2011 at 9:03
4

Bash doesn't use any of the above kinds of regular expressions in the if and the case statements.

Rather, it uses shell glob patterns, which are less powerful than any kind of regular expresion. Relatively new versions of bash also allow the use of extended glob patterns which are expressively equal to regular expressions without (line) anchors but including negated terms, but with a syntax different from any of the above mentioned regular expression types.

Note however that extended shell globs must be enabled prior use with shopt -s extglob both in a shell script or interactively.

In a single exceptional place, namely within the [[ ]] conditional command, the =~ binary relation allows to check a string on the left side against an extended regular expression ERE on the right side.

1

According to the linked Bash guide, Bash (since version 3.0+) uses: "the Extended Regular Expression (ERE) dialect."

More information on that dialect can be found here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .