Em Ae
Em Ae

Reputation: 8734

String replacement in bash script giving error

I am trying to replace a particular character in string using bash script but I am failing.

I have following code

    line=${array[1]}
    echo ${array[1]}
    echo ${array[0]}
    echo `expr index "$line" *`

The line or array[1] contains following string /path/v1/module/order/* and I want to replace * with some input value from another file.

But i got error at last line ... I tried with line variable and even with array. The error was expr: syntax error

P.S: I am using bash version 3

Upvotes: 0

Views: 84

Answers (2)

glenn jackman
glenn jackman

Reputation: 247210

Just using bash parameter expansion

line='/path/v1/module/order/*'
repl='some other value'
newvalue=${line/\*/$repl}
echo "$newvalue"
/path/v1/module/order/some other value

Upvotes: 2

chepner
chepner

Reputation: 532333

The unquoted asterisk is expanded to a list of file names before expr is called. Use

echo $( expr index "$line" "*" )

(The $(...) is not necessary, but recommended in place of backquotes.)

Upvotes: 1

Related Questions