expand unix variable inside sed command [duplicate

2019-08-12 16:53发布

This question already has an answer here:

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

like

file_name=abc.txt

needs to be replaced like

file_name=xyz.txt

where $file=xyz.txt

I tried

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

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

any pointers?

标签: linux unix sed
1条回答
Melony?
2楼-- · 2019-08-12 17:18

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

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

Output:

file_name=xyz.txt

查看更多
登录 后发表回答