bug-bash
[Top][All Lists]
Advanced

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

Re: =~ behaves differently in bash 3.2 and 3.0


From: Chet Ramey
Subject: Re: =~ behaves differently in bash 3.2 and 3.0
Date: Fri, 24 Oct 2008 13:54:19 -0400
User-agent: Thunderbird 2.0.0.17 (Macintosh/20080914)

Clark J. Wang wrote:
> In bash 3.0.14, the condition [[ file.txt =~ .*\\.txt\$ ]] returns TRUE but
> in 3.2.39 it returns FALSE. But with the shopt option `compat31' set it also
> returns TRUE. Is that reasonable? In the bash manual, `compat31' makes sense
> only for quoted patterns. The string .*\\.txt\$ is considered to be quoted?

The manual says that any part of the pattern may be quoted to force
it to be matched as a string.  This is the behavior that compat31
turns off.  The backslash is one of the shell's quoting characters, so
portions of the pattern above are quoted.  The question is how the shell
processes those quoted characters.

When compat31 is off, during expansion, the shell converts the
characters quoted using shell quoting in a way that removes their
special meaning from the regexp matcher.  This forces the characters
to match themselves.  The pattern passed to the regular expression
matcher is exactly ".*\\.txt\$", since the backslash is also how one
quotes characters to the regexp matcher

When compat31 is enabled, the shell performs all word expansions on
the pattern, including quote removal, and passes the result to the
regexp matcher.  In this case, the pattern passed is ".*\.txt$", which
is what you had in mind.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer

Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/




reply via email to

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