2013-04-30 66 views
17

我怎样才能做到这一点替换文本?具有可变

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

它将用文本$host_name替换wiki_host。 但我想用变量的内容来代替它..

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

试了一下它也不起作用。

+0

检查:http://theunixshell.blogspot.com/2013/04/replace-string-in-file-with-value-in.html – Vijay 2013-04-30 10:20:21

回答

32

您需要用双引号:

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

你的单引号阻止shell变量从它的内容所取代。

+1

非常感谢您! :) – Vince 2013-04-30 09:58:32