Celeb Glow
news | March 30, 2026

Using sed with a variable inside double quote

I have a file input.xml which contains a line: <exciton lambda="1" fix="hole"/>

in that line I want to replace 1 with 2 but I can't use just 1 as key word since there are other instances of "1" in the same file so I have to use the keyword lambda="1"

I am trying to use sed command as below:

sed -i "s/lambda="1"/lambda="$value"/g" input.xml

But it's not working, can anybody please help.

2 Answers

First of all, it is never a good idea to try to parse XML or other such structured text with regular expressions. While theoretically possible to do well and safely, it is extremely hard even for experts. Far better to use a dedicated tool that is designed to handle the language you have. So if this is an XML file, you could use something like xmlstarlet instead. For example, given this input file:

$ cat file.xml
<?xml version="1.0"?>
<record> <exciton lambda="1" fix="hole"/>
</record>

You could do:

$ value="1234"
$ xml ed -u "//record[@id=1]/exciton/@lambda" -v "$value" file.xml
<?xml version="1.0"?>
<record> <exciton lambda="1234" fix="hole"/>
</record>

All that said, with the very minimal information you have given us, it is possible that you can do it using a simple, naive parser like sed. There are various tricks you can try. First, just escape the double quotes:

$ sed "s/lambda=\"1\"/lambda=\"$value\"/" file.xml
<?xml version="1.0"?>
<record> <exciton lambda="1234" fix="hole"/>
</record>

Or, use single quotes but end them and insert the quoted variable outside the single quotes. Then, open a new single quotes sting to continue the sed expression:

$ sed 's/lambda="1"/lambda="'"$value"'"/' file.xml
<?xml version="1.0"?>
<record> <exciton lambda="1234" fix="hole"/>
</record>

That one is a bit hard to parse visually, but this is what it is doing with added whitespace for legibility (note that it doesn't actually work with the added whitespace, this is only for clarity):

sed 's/lambda="1"/lambda="' "$value" '"/' file.xml

You have two options. First, you can simply escape the double

I am trying to use sed command as below :

sed -i "s/lambda="1"/lambda="$value"/g" input.xml

Assuming value is already defined, enclose the variable with single quotes: '$value':

sed -i 's/lambda="1"/lambda="'$value'"/g' input.xml

EDIT as @terdon pointed, if your value variable contains whitespace the above will not work and you will need to double-quote the variable within the single quotes:

sed -i 's/lambda="1"/lambda="'"$value"'"/g' input.xml

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy