在 sed 命令中扩展 unix 变量 [英] expand unix variable inside sed command

查看:44
本文介绍了在 sed 命令中扩展 unix 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用分配给变量的新值替换配置文件中的当前值,

I need to replace current value in configuration file with new value which is assigned to variable ,

喜欢

file_name=abc.txt

需要像

file_name=xyz.txt

其中 $file=xyz.txt

我试过了

sed -i 's/file_name=.*/file_name=$file/g'  conf_file.conf

但是变量没有被扩展,它就像 file_name=$file.

however the variable is not getting expanded, it comes like file_name=$file.

有什么指点吗?

推荐答案

这应该可以工作,假设变量 file 具有分配给它的值:xyz.txt:

This should work,assuming that variable file has value:xyz.txt assigned to it:

sed "s/file_name=.*/file_name=${file}/g" file_name

输出:

file_name=xyz.txt

这篇关于在 sed 命令中扩展 unix 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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