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 =~

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 2018-07-10 09:09 -0400
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>

Show all headers | 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