Reputation: 33
I meet such error:
expr: syntax error
and my script is:
#!/bin/bash
echo `expr index "sarasara" a`
how to fix it.
Upvotes: 1
Views: 938
Reputation: 31
try this, use shell function to simulate "expr index string substring" :
index() {
str="$1"
substr="$2"
var=${str%%$substr*}
echo "(${#var} + 1 ) % (${#str} + 1)" |bc
}
Upvotes: 0
Reputation: 522519
$ man expr
gives this at the end:
According to the POSIX standard, the use of string arguments
length
,substr
,index
, ormatch
produces undefined results. In this version ofexpr
, these arguments are treated just as their respective string values.
In other words, macOS' expr
does not support index
.
Upvotes: 2