views:

53

answers:

1

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?

+6  A: 
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.

Matthew Flaschen
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
@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 `\\\$`.
Matthew Flaschen
@Matthew: Thanks for the correction.
Philipp