17

I execute the following bash script:

#!/bin/bash
version=$1
echo $version
sed 's/\${version.number}/$version/' template.txt > readme.txt

I'm expecting to replace all instances of ${version.number} with the contents of the variable "version". Instead the literal text $version is being inserted.

What do I need to do to make sed use the current value of $version instead?

0

3 Answers 3

22
sed "s/\${version.number}/$version/" template.txt > readme.txt

Only double quotes do dollar-sign replacement. That also means single quotes don't require the dollar sign to be escaped.

Sign up to request clarification or add additional context in comments.

3 Comments

But the dollar sign has to be escaped because the expression is treated as a regex by sed. So I think it should be \\$.
@Philipp, actually the $ sign is only an anchor when it's the last character of the regex (or possibly subexpression). But I admit I got lucky. ;) You can optionally add an escaped slash, but then it's \\\$.
This worked better for me.
4

You could also simply unquote the variables

sed 's/'${version.number}'/'$version'/' template.txt > readme.txt

Comments

0

It's a bit old, but it might still be helpful... For me it worked using a double escape as Philip said (and escaping parenthesis, if you use them...):

#!/bin/bash
LIVE_DB_NAME='wordpress';
STAGING='staging';
sed -r "s/define\('DB_NAME', '[a-zA-Z0-9]+'\);/define('DB_NAME', '\\${LIVE_DB_NAME}');/" ${STAGING}/wp-config.php >tmp1;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.