kailee
kailee

Reputation: 33

macOS Mojave version 10.14.1 bash-3.2 expr: syntax error

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

Answers (2)

gao204
gao204

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

deceze
deceze

Reputation: 522519

$ man expr gives this at the end:

According to the POSIX standard, the use of string arguments length, substr, index, or match produces undefined results. In this version of expr, these arguments are treated just as their respective string values.

In other words, macOS' expr does not support index.

Upvotes: 2

Related Questions