[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [[ str =~ "^pattern" ]] no longer works
From: |
Greg Wooledge |
Subject: |
Re: [[ str =~ "^pattern" ]] no longer works |
Date: |
Fri, 4 Nov 2011 13:47:47 -0400 |
User-agent: |
Mutt/1.4.2.3i |
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.