Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a text with a variable [duplicate]

How can I do this?

sed -i 's/wiki_host/$host_name/g' /root/bin/sync

It will replace wiki_host with the text $host_name. But I want to replace it with the content of the variable..

I tried it with

sed -i 's/wiki_host/${host_name}/g' /root/bin/sync

It doesn't work either.

like image 411
Vince Avatar asked Apr 30 '13 09:04

Vince


1 Answers

You need to use double quotes:

$ sed -i "s/wiki_host/${host_name}/g" /root/bin/sync

Your single quotes prevent the shell variable from being replaced with its contents.

like image 75
Andreas Fester Avatar answered Nov 01 '22 12:11

Andreas Fester