sed 用双引号替换变量 [英] Sed replace variable in double quotes

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

问题描述

我创建了一个带有参数的 bash 脚本.我想将该参数传递给 sed 以用另一个由变量组成的字符串替换现有字符串:

I've created a bash script that takes a parameter. I want to pass that parameter to sed to replace an existing string with another which is composed of the variable:

variable=$1
echo $variable
sed -i -e 's/name="master"/name="$variable"/g' test

问题是脚本没有用参数替换 $variable,它只是用 "$variable" 替换字符串:

The problem is that the script is not replacing $variable with the parameter, it's just replacing the string with "$variable":

<host name=""$variable"" xmlns="urn:jboss:domain:3:0:>

如何用变量替换引号中的字符串?

推荐答案

变量扩展不会发生在单引号内.用双引号做:

Variable expansion does not happen within single quotes. Do it in double quotes:

sed -i -e 's/name="master"/name="'"$variable"'"/g' test

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

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