Ken J
Ken J

Reputation: 4582

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?

Upvotes: 10

Views: 17942

Answers (3)

farrell47
farrell47

Reputation: 1

just do like this:

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

Upvotes: -3

Sven Marnach
Sven Marnach

Reputation: 602635

Variable expansion does not happen within single quotes. Do it in double quotes:

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

Upvotes: 22

Avinash Raj
Avinash Raj

Reputation: 174844

Change your code like his,

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

Upvotes: 3

Related Questions