Linux 用变量替换文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16297052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:49:27 来源:igfitidea点击:
Replace a text with a variable
提问by Vince
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..
它将替换wiki_host
为文本$host_name
。但我想用变量的内容替换它..
I tried it with
我试过了
sed -i 's/wiki_host/${host_name}/g' /root/bin/sync
It doesn't work either.
它也不起作用。
采纳答案by Andreas Fester
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.
您的单引号可防止 shell 变量被其内容替换。