Reputation: 10468
I have: test.txt:
version-1
version-1
ori.sh:
old="version-1"
new="version-2"
sed -i .bak 's/${old}/${new}/g' test.txt
when running ori.sh, nothing happens. I would expect that test.txt would look like:
test.txt*:
version-2
version-2
Any ideas?
Upvotes: 2
Views: 7498
Reputation: 15058
You need to double quote out your variables.
The following works for me:
old="version-1"
new="version-2"
sed -i.bck 's:'"${old}"':'"${new}"':g' test.txt
Upvotes: 4
Reputation: 785196
Single quotes is the problem. bash (or other shells) don't expand variables in single quotes.
Use this sed command with double quotes so that shell can expand variables:
sed -i.bak "s/${old}/${new}/g" test.txt
Upvotes: 10