On Fri, Nov 04, 2011 at 05:30:52PM +0100, Roman Rakus wrote:
> On 11/04/2011 09:09 AM, flong@dell1.localdomain wrote:
> >     [[ "-h" =~ '^-h' ]] ; echo $?
> >     Should return 0, but instead returns 1.
> >
> It was bug in previous versions of bash in fedora and RHEL.
> This behavior is correct now.

Expanding this slightly: when a character in the operand on the
right hand side of the =~ operator is quoted, that character no
longer retains its Regular Expression meaning, and instead becomes
a string literal.

This also applies to quoted substitutions, as in [[ $a =~ "$b" ]].
If you want the value of $b to be treated as an ERE, don't quote it.

[[ "-h" =~ '^-h' ]] compares the string -h to the string ^-h and of
course they are not equal.

[[ "-h" =~ ^-h ]]   does what you expected.

re='^-h'
[[ "-h" =~ $re ]]   is the preferred way to write the code.  It's even
written in the manual this way.

Reply via email to