First check if Bash even has an RE implementation of its own, or if it just uses one from the system libraries.
But yeah, \b
is from Perl regexes, and in general, they also contain piles of other extensions that aren't available in standard regexes. Though GNU systems seem to support \s
for whitespace and \w
for word chars, but not \d
for digits. I don't know why they've decided to pick those odd ones, but in general, having all Perl RE features would likely make the RE engine much more complicated, and even though Perl fans might love it, many of the standard tools' authors might not want that. And then if you start adding this and that, but not all, deciding where to draw the line becomes an issue.
In any case, word borders are non-standard to begin with. On a few systems, \<
and \>
should work for the left and right borders, while on FreeBSD and Mac, you need [[:<:]]
and [[:>:]]
.
As it happens, like @steeldriver comments, \b
also seems to work GNU, at least as I tested. Just that in Bash you need to store the RE in a variable first, to avoid the special chars getting mashed by the shell's parsing processes:
$ re='\bWORD\b'; if [[ WORD =~ $re ]]; then echo y; else echo n; fi
y
$ re='\<WORD\>'; if [[ WORD =~ $re ]]; then echo y; else echo n; fi
y
$ re='\bWORD\b'; if [[ WORDLESS =~ $re ]]; then echo y; else echo n; fi
n
$ re='\<WORD\>'; if [[ WORDLESS =~ $re ]]; then echo y; else echo n; fi
n
\bWORD\b
is subject to quote removal in the[[...]]
context? Have you tried settingre='\bWORD\b'
then[[ $foo =~ $re ]]
for example? – steeldriver Mar 18 '24 at 12:25.
,*
,()
, and concatenation is an extension that differs between implementations. – davolfman Mar 18 '24 at 18:12