bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Inconsistence when checking if a pattern is quoted or not for `==' a


From: Davide Brini
Subject: Re: Inconsistence when checking if a pattern is quoted or not for `==' and `=~' in [[ ]]
Date: Thu, 17 Feb 2011 14:46:45 +0000
User-agent:

On Thu, 17 Feb 2011 22:56:21 +0800
"Clark J. Wang" <address@hidden> wrote:

> On Thu, Feb 17, 2011 at 7:09 PM, Clark J. Wang <address@hidden> wrote:
> 
> > See following script output:
> >
> > bash-4.2# cat quoted-pattern.sh
> > [[ .a == \.a* ]] && echo 1  # not quoted
> > [[ aa =~ \.a* ]] && echo 2  # quoted
> >
> > [[ aa =~ \a.  ]] && echo 3  # not quoted
> > [[ aa =~ \a\. ]] && echo 4  # quoted
> > bash-4.2# bash42 quoted-pattern.sh
> > 1
> > 3
> > bash-4.2#
> >
> > From my understanding 1 2 3 4 should all be printed out.
> >
> >
> The point is: ``Any part of the pattern may be quoted to force  it  to  be
> matched  as  a string.'' And backslash is one of bash's quoting chars. But
> in my examples, a pattern with `\' in it sometimes is considered to be
> quoted and sometimes unquoted. It's not clear to me what's the exact rule
> to tell if a pattern is quoted or not.

In your first case, the dot is quoted (and taken literally). The lhs is ".a"
(literal), so overall you have a match.

Second case: again you're quoting the dot, but the lhs now is "aa", which
doesn't match "dot followed by a followed by any number of characters", so
no match.

Third case: you're quoting "a" (which wouldn't need to be quoted because
it's not special, but then the backslash is discarded). The dot however is
not quoted, so it has the usual regexp meaning of "any character". Thus
"aa" matches the regular expression pattern "a.".

Fourth case: you're quoting both the "a" and the dot, which makes the
patter to match against literally "a followed by dot". The input is "aa",
which doesn't match.

Hope this helps.

-- 
D.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]