简体   繁体   English

Sed用双引号替换变量

[英]Sed replace variable in double quotes

I've created a bash script that takes a parameter. 我创建了一个带参数的bash脚本。 I want to pass that parameter to sed to replace an existing string with another which is composed of the variable: 我想将该参数传递给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? 如何用变量替换引号中的字符串?

Variable expansion does not happen within single quotes. 单引号内不会发生变量扩展。 Do it in double quotes: 用双引号做:

sed -i -e 's/name="master"/name="'"$variable"'"/g' test

像他一样改变你的代码,

sed -i -e 's/name="master"/name="'"$variable"'"/g' test

just do like this: 就像这样:

var=apple
sed -i "s/pineapple/$"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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