[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [[ ... =~ ... ]] is broken when RHS is quoted
From: |
Pierre Gaston |
Subject: |
Re: [[ ... =~ ... ]] is broken when RHS is quoted |
Date: |
Sun, 22 Jun 2008 21:29:40 +0300 |
On Sun, Jun 22, 2008 at 8:15 PM, Alexis Huxley <ahuxley@gmx.net> wrote:
>> > Description:
>> > [[ ... =~ ... ]] is broken when RHS is quoted
>>
>> from http://tiswww.case.edu/php/chet/bash/CHANGES :
>>
>> f. Quoting the string argument to the [[ command's =~ operator now forces
>> string matching, as with the other pattern-matching operators.
>
> Hmmm ... ok, thanks, I did miss that. While I think that that is
> reasonably clear, I think the man page is not:
>
> Word splitting and pathname expansion are not performed on
> the words between the [[ and ]]; tilde expansion,
> parameter and variable expansion, arithmetic expansion,
> command substitution, process substitution, and quote removal
> are performed. ^^^^^^^^^^^^^
>
> "Quote removal" means that, as usual, quotes do not form part of the
> arguments, they merely serve to delimit the arguments, I take it.
> "Words between [[ and ]] ... quote removal performed" means on *all*
> words between [[ and ]] I take it. Hmm ... No, that can't be right
> otherwise
>
> bash -c '[[ "apple" =~ "(apple)" ]]; echo ${BASH_REMATCH[1]}'
>
> would say apple. Hmmm ... while the CHANGES file indicates that I
> was wrong that bash is broken, I would say that bash is broken
> *w.r.t. the behaviour documented in its man page*. What do you think?
>
> Alexis
Well, quote removal is performed, it's just that the "active" part of
the regexp must not be inside the quotes ie:
bash -c '[[ apple =~ ("apple") ]]; echo ${BASH_REMATCH[1]}'
a simple way for your regexp to be backward compatible is to store it
in a variable:
bash -c 'reg="(apple)";[[ apple =~ $reg ]]; echo ${BASH_REMATCH[1]}'