Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14310

Re: Word boundary anchors \< and \> not parsed correctly on the right side of =~

Path csiph.com!goblin2!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail
From Greg Wooledge <wooledg@eeg.ccf.org>
Newsgroups gnu.bash.bug
Subject Re: Word boundary anchors \< and \> not parsed correctly on the right side of =~
Date Tue, 10 Jul 2018 09:09:06 -0400
Lines 38
Approved bug-bash@gnu.org
Message-ID <mailman.3369.1531228156.1292.bug-bash@gnu.org> (permalink)
References <5b440fe8.1c69fb81.948f6.4d1e@mx.google.com> <20180710122718.wrrzjuqpmpns4o4o@eeg.ccf.org> <20180710130234.r5mwjc62adids7m5@monk>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=iso-8859-1
Content-Transfer-Encoding 8bit
X-Trace usenet.stanford.edu 1531228156 29795 208.118.235.17 (10 Jul 2018 13:09:16 GMT)
X-Complaints-To action@cs.stanford.edu
Cc bug-bash@gnu.org, bash@packages.debian.org
To Paulo Marcel Coelho Aragão <marcelpaulo@gmail.com>
Envelope-to bug-bash@gnu.org
Mail-Followup-To Paulo Marcel Coelho Aragão <marcelpaulo@gmail.com>, bug-bash@gnu.org, bash@packages.debian.org
Content-Disposition inline
In-Reply-To <20180710130234.r5mwjc62adids7m5@monk>
User-Agent NeoMutt/20170113 (1.7.2)
X-detected-operating-system by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy]
X-Received-From 139.137.100.1
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.21
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <http://lists.gnu.org/archive/html/bug-bash/>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
Xref csiph.com gnu.bash.bug:14310

Show key headers only | View raw


On Tue, Jul 10, 2018 at 10:02:34AM -0300, Paulo Marcel Coelho Aragão wrote:
> It baffles me that literal \< \> are not evaluated correctly
> 
>     paulo:~$ [[ 'foo bar' =~ \<foo\> ]] && echo yes || echo no
>     no

In this context, the backslashes serve only to "quote" the less-than
and greater-than signs.  It's just like writing

  [[ 'foo bar' =~ "<"foo">" ]]

And when you quote a character on the right-hand side of = or =~ inside
a [[ command, that character LOSES whatever specialness it might have
had in that context.

In order to put a backslash-that-acts-like-a-backslash-to-the-regex-engine
inside the RE on the right-hand side of =~ you need to put the RE into a
variable.

(Normally this is not a problem because normally you write POSIX
ERE-compliant regexes because that's all that you can count on, and POSIX
EREs don't use backslashes to turn features ON.  Only to turn them OFF.)

> Empirically all this demonstrates:
> 
> 1. bash recognizes \< \> \b as word anchors

Only on some systems.  Not on others.  So don't do that.

> 2. bash doesn't evaluate them correctly when used as literals
> 3. bash evaluates them correctly when used with parameter expansion

Which is one of the reasons it is STRONGLY encouraged to put your RE
into a variable and then use =~ $var.

(Another reason is backward compatibility with bash 3.x versions, should
you happen to care about that.)

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: Word boundary anchors \< and \> not parsed correctly on the right side of =~ Greg Wooledge <wooledg@eeg.ccf.org> - 2018-07-10 09:09 -0400

csiph-web