0

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

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.