bash `=~" operator

bash `=~" operator

am 20.01.2008 01:20:16 von Harry Putnam

Where can I find some documentation of syntax for the right side of
bash =~ operator?

I didn't see enough about `=~' in the bash manual to let me understand
how to use it syntactically. I mean about the usage of quotes when
the regex used will cause metacharacter or shell expansion problems.

Example:
Maybe a poor example since this is not likely to happen but
I actually tried to do something like this in a script and ran
into a problem

--------->8 snip ---------
#!/bin/bash

if [[ $1 =~ ^ *\-*r*[bm] *$ ]];then
echo "\`<$1> MATCH using =~ operator'"
fi

if echo $1 |grep "^ *\-*r*[bm] *$" >/dev/null;then
echo "\`<$1> MATCH using grep'"
fi
--------->8 snip ---------

With regex left unquoted as above: (^ *\-*[bm] *$).
Of course it causes a failure.
./test -rb

./test: line 3: syntax error in conditional expression
./test: line 3: syntax error near `*\-*r*[bm]'
./test: line 3: `if [[ $1 =~ ^ *\-*r*[bm] *$ ]];then'

===================================
If I quote the regex ("^ *\-*r*[bm] *$") it doesn't match when it
should:
./test -rb

`<-rb> MATCH using grep'

===================================

Can anyone show an example or two to help establish a sort of
`rule of thumb' for the right side of `=~' operator using recent
bash?

Re: bash `=~" operator

am 20.01.2008 08:25:47 von pgas

reader@newsguy.com wrote:

> --------->8 snip ---------
> #!/bin/bash
>
> if [[ $1 =~ ^ *\-*r*[bm] *$ ]];then
> echo "\`<$1> MATCH using =~ operator'"
> fi
You need to quote the spaces:

[[ $1 =~ ^" "*\-*r*[bm]" "*$ ]]

or easier (and backward compatible with bash3.1):

regexp='^ *\-*r*[bm] *$'
[[ $1 =~ $regexp ]]

--
pgas @ SDF Public Access UNIX System - http://sdf.lonestar.org

Re: bash `=~" operator

am 20.01.2008 11:25:17 von Stephane CHAZELAS

On Sun, 20 Jan 2008 07:25:47 +0000 (UTC), pgas wrote:
> reader@newsguy.com wrote:
>
>> --------->8 snip ---------
>> #!/bin/bash
>>
>> if [[ $1 =~ ^ *\-*r*[bm] *$ ]];then
>> echo "\`<$1> MATCH using =~ operator'"
>> fi
> You need to quote the spaces:
>
> [[ $1 =~ ^" "*\-*r*[bm]" "*$ ]]
>
> or easier (and backward compatible with bash3.1):
>
> regexp='^ *\-*r*[bm] *$'
> [[ $1 =~ $regexp ]]

or

if expr "x$1" : 'x *-*r*[bm] *$' > /dev/null; then

and it's POSIX and even backward compatible with the Bourne
shell.

--
Stephane

Re: bash `=~" operator

am 20.01.2008 15:50:00 von Harry Putnam

Stephane Chazelas writes:

> On Sun, 20 Jan 2008 07:25:47 +0000 (UTC), pgas wrote:
>> reader@newsguy.com wrote:
>>
>>> --------->8 snip ---------
>>> #!/bin/bash
>>>
>>> if [[ $1 =~ ^ *\-*r*[bm] *$ ]];then
>>> echo "\`<$1> MATCH using =~ operator'"
>>> fi
>> You need to quote the spaces:
>>
>> [[ $1 =~ ^" "*\-*r*[bm]" "*$ ]]
>>
>> or easier (and backward compatible with bash3.1):
>>
>> regexp='^ *\-*r*[bm] *$'
>> [[ $1 =~ $regexp ]]
>
> or
>
> if expr "x$1" : 'x *-*r*[bm] *$' > /dev/null; then
>
Probably a dumb question but is using `=~' not POSIX?

`=~' is more consistent with some other scripting languages like ksh,
maybe zsh (not sure about zsh), awk and perl.

Re: bash `=~" operator

am 20.01.2008 15:50:56 von Harry Putnam

pgas writes:

> You need to quote the spaces:
>
> [[ $1 =~ ^" "*\-*r*[bm]" "*$ ]]
>
> or easier (and backward compatible with bash3.1):
>
> regexp='^ *\-*r*[bm] *$'
> [[ $1 =~ $regexp ]]

Ahh.. thanks. There's the rule of thumb I was after.

Re: bash `=~" operator

am 20.01.2008 18:57:41 von Stephane CHAZELAS

On Sun, 20 Jan 2008 08:50:00 -0600, reader@newsguy.com wrote:
[...]
>> if expr "x$1" : 'x *-*r*[bm] *$' > /dev/null; then
>>
> Probably a dumb question but is using `=~' not POSIX?
>
> `=~' is more consistent with some other scripting languages like ksh,
> maybe zsh (not sure about zsh), awk and perl.

No, it's a very recent feature of bash, zsh and ksh93.

It was introduced in bash 3 (August 2004), ksh93s (December
2006), and zsh 4.3.4-dev2 (May 2007).

ksh93 and zsh had other ways of doing regexp pattern matching
long before that.

IIRC, [[ =~ ]] was available as a patch to bash by William Park
before that.

The syntax is probably slightly different in all of those
shells. Though I think they all agree to use EREs.

var=pattern
[[ $string =~ $var ]]

is likely to work consistently in all those shells as long as
pattern is a standard ERE pattern.

--
Stephane