Shell - sed not replacing variables

Hi,

Im using this sed command in order to replace a line in the hosts file but as I run the script it looks like its not replacing the variables per their value...

sed -i 's/${host_db}/${new_db}   db-01/' /etc/hosts

Running using sh -x I get this:
+ new_db=10.216.193.68
+ host_db='12.18.1.61   db-01'
+ sed -i 's/${host_db}/${new_db}   db-01/' /etc/hosts


Anything I might be missing?

Tks,
Joao
joaotellesAsked:
Who is Participating?
 
woolmilkporcCommented:
Or

sed -i "s/${host_db}/${new_db}   db-01/" /etc/hosts

Background:

The shell does not resolve variables enclosed by single quotes, so you must either take them out of the quotes (first solution) or use double quotes instead (second solution) - if there is nothing which shall be protected from the shell intentionally, that is.
0
 
woolmilkporcCommented:
sed -i 's/'${host_db}'/'${new_db}'   db-01/' /etc/hosts
0
 
joaotellesAuthor Commented:
Tks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.