如何在 sed 中使用 bash 脚本变量 [英] How to use a bash script variable with sed

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

问题描述

我执行以下 bash 脚本:

I execute the following bash script:

#!/bin/bash
version=$1
echo $version
sed 's/${version.number}/$version/' template.txt > readme.txt

我希望用变量version"的内容替换 ${version.number} 的所有实例.相反,插入的是文字文本 $version.

I'm expecting to replace all instances of ${version.number} with the contents of the variable "version". Instead the literal text $version is being inserted.

我需要做什么才能让 sed 使用 $version 的当前值?

What do I need to do to make sed use the current value of $version instead?

推荐答案

sed "s/${version.number}/$version/" template.txt > readme.txt

只有双引号可以替换美元符号.这也意味着单引号不需要转义美元符号.

Only double quotes do dollar-sign replacement. That also means single quotes don't require the dollar sign to be escaped.

这篇关于如何在 sed 中使用 bash 脚本变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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