oriharel
oriharel

Reputation: 10468

Shell script - sed doesn't work

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

Answers (2)

Ewan
Ewan

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

anubhava
anubhava

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

Related Questions