具有可变替换文本(SED) [英] Replace a text with a variable (sed)

查看:107
本文介绍了具有可变替换文本(SED)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能做到这一点?

How can I do this?

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

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

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

我试了一下

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

它不工作了。

推荐答案

您需要用双引号:

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

您单引号prevent从它的内容被替换的shell变量。

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

这篇关于具有可变替换文本(SED)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆