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


Groups > gnu.bash.bug > #14310 > unrolled thread

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

Started byGreg Wooledge <wooledg@eeg.ccf.org>
First post2018-07-10 09:09 -0400
Last post2018-07-10 09:09 -0400
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

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

FromGreg Wooledge <wooledg@eeg.ccf.org>
Date2018-07-10 09:09 -0400
SubjectRe: Word boundary anchors \< and \> not parsed correctly on the right side of =~
Message-ID<mailman.3369.1531228156.1292.bug-bash@gnu.org>
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.)

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web