用变量替换文本 [英] Replace a text with a variable

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

问题描述

我该怎么做?

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

它也不起作用.

解决方案

您需要使用双引号:

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

您的单引号可防止将shell变量替换为其内容.

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.

解决方案

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.

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

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