I've created a bash script that takes a parameter. I want to pass that parameter to sed to replace an existing string with another which is composed of the variable:
我创建了一个带参数的bash脚本。我想将该参数传递给sed以将现有字符串替换为由变量组成的另一个字符串:
variable=$1
echo $variable
sed -i -e 's/name="master"/name="$variable"/g' test
The problem is that the script is not replacing $variable with the parameter, it's just replacing the string with "$variable":
问题是脚本没有用参数替换$ variable,它只是用“$ variable”替换字符串:
<host name=""$variable"" xmlns="urn:jboss:domain:3:0:>
How can I replace a string in quotes with the variable?
如何用变量替换引号中的字符串?
3 个解决方案
#1
9
Variable expansion does not happen within single quotes. Do it in double quotes:
单引号内不会发生变量扩展。用双引号做:
sed -i -e 's/name="master"/name="'"$variable"'"/g' test
#2
3
Change your code like his,
像他一样改变你的代码,
sed -i -e 's/name="master"/name="'"$variable"'"/g' test
#3
-2
just do like this:
就这样做:
var=apple
sed -i "s/pineapple/$"
#1
9
Variable expansion does not happen within single quotes. Do it in double quotes:
单引号内不会发生变量扩展。用双引号做:
sed -i -e 's/name="master"/name="'"$variable"'"/g' test
#2
3
Change your code like his,
像他一样改变你的代码,
sed -i -e 's/name="master"/name="'"$variable"'"/g' test
#3
-2
just do like this:
就这样做:
var=apple
sed -i "s/pineapple/$"