bug-bash
[Top][All Lists]
Advanced

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

Re: bash=~ bug or feature


From: Chet Ramey
Subject: Re: bash=~ bug or feature
Date: Thu, 17 May 2007 13:55:03 -0400

> With bash 3.1.17(4)-release
> # [[ "abcd" =~ "^a" ]]; echo $?
> 0
> 
> With bash 3.2.17(3)-release
> # [[ "abcd" =~ "^a" ]]; echo $?   # is this a bug???
> 1
> # [[ "abcd" =~ ^a ]]; echo $?
> 0
> 
> Is this a bug?

It is the intended behavior.  See question E14 in the latest bash FAQ
(reproduced below).  There has also been extensive discussion of this
issue on bug-bash; see the list archives for a record.

E14) Why does quoting the pattern argument to the regular expression matching
     conditional operator (=~) cause regexp matching to stop working?

In versions of bash prior to bash-3.2, the effect of quoting the regular
expression argument to the [[ command's =~ operator was not specified.
The practical effect was that double-quoting the pattern argument required
backslashes to quote special pattern characters, which interfered with the
backslash processing performed by double-quoted word expansion and was
inconsistent with how the == shell pattern matching operator treated
quoted characters.

In bash-3.2, the shell was changed to internally quote characters in single-
and double-quoted string arguments to the =~ operator, which suppresses the
special meaning of the characters special to regular expression processing
(`.', `[', `\', `(', `), `*', `+', `?', `{', `|', `^', and `$') and forces
them to be matched literally.  This is consistent with how the `==' pattern
matching operator treats quoted portions of its pattern argument.

Since the treatment of quoted string arguments was changed, several issues
have arisen, chief among them the problem of white space in pattern arguments
and the differing treatment of quoted strings between bash-3.1 and bash-3.2.
Both problems may be solved by using a shell variable to hold the pattern.
Since word splitting is not performed when expanding shell variables in all
operands of the [[ command, this allows users to quote patterns as they wish
when assigning the variable, then expand the values to a single string that
may contain whitespace.  The first problem may be solved by using backslashes
or any other quoting mechanism to escape the white space in the patterns.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                                Live Strong.
Chet Ramey, ITS, CWRU    chet@case.edu    http://tiswww.tis.case.edu/~chet/




reply via email to

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