Shell 脚本 - sed 不起作用 [英] Shell script - sed doesn't work

查看:178
本文介绍了Shell 脚本 - sed 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:测试.txt:

version-1
version-1

ori.sh:

old="version-1"
new="version-2"
sed -i .bak 's/${old}/${new}/g' test.txt

运行 ori.sh 时,没有任何反应.我希望 test.txt 看起来像:

when running ori.sh, nothing happens. I would expect that test.txt would look like:

test.txt*:

version-2
version-2

有什么想法吗?

推荐答案

单引号是问题所在.bash(或其他 shell)不要用单引号扩展变量.

Single quotes is the problem. bash (or other shells) don't expand variables in single quotes.

使用带双引号的这个 sed 命令,以便 shell 可以扩展变量:

Use this sed command with double quotes so that shell can expand variables:

sed -i.bak "s/${old}/${new}/g" test.txt

这篇关于Shell 脚本 - sed 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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