简体   繁体   中英

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

PS: I am using bash version 3

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM