2014-01-06 66 views
0

我想用bash脚本替换字符串中的特定字符,但是我失败了。bash脚本中的字符串替换给出错误

我有以下代码

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

线或阵列[1]包含以下字符串/path/v1/module/order/*,我想与来自另一文件中的一些输入值来代替*

但我在最后一行发生错误...我尝试使用行变量,甚至与数组。错误是 expr: syntax error

P.S:我使用的是bash版本3

回答

2

只需使用bash parameter expansion

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

优秀....... 。 –

1

的不带引号的星号被扩展到文件名列表expr被调用之前。使用

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

(该$(...)是没有必要的,但在地方反引号的建议。)