Aaron
Aaron

Reputation: 65

expr match problem in shell

In an sh shell script I wrote the following:

opr=+  
echo `expr match "$opr" '[+\-x/]'`  

but I get this error when ran:

expr: syntax error  

What am I doing wrong? I get the same error when I make opr equal to - and / .

Another interesting thing I found is when I wrote this:

opr=a  
echo `expr match "$opr" '[+\-x/]'`  

it returns this:

1  

This means that it matched the string "a" to one of +, -, x, and /. But that makes no sense!

Upvotes: 0

Views: 1150

Answers (1)

evil otto
evil otto

Reputation: 10582

First case: +

+ has a special meaning to expr:

   + TOKEN
          interpret TOKEN as a string, even if it is a
          keyword like `match' or an operator like `/'

Second case: a

your regexp is a range operation, matching characters from + to x, which includes most alnums. To make the - be matched literally in a charclass, it must be the first or last character; backslashing it doesn't work.

Upvotes: 1

Related Questions