简体   繁体   中英

Sed replace variable in double quotes

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:

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":

<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/$"

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