开发者

Problem with quoting and sed

开发者 https://www.devze.com 2023-04-06 16:46 出处:网络
I\'m trying to find and replacing some text using sed. Specifically, I开发者_如何学运维\'m trying to add quotes around a variable which could contain whitespaces.

I'm trying to find and replacing some text using sed. Specifically, I开发者_如何学运维'm trying to add quotes around a variable which could contain whitespaces.

sed -i 's/$VAR some.file/"$VAR" some.file/g' path/to/file

Unfortunately, the result is not expected. I've also tried to backslash the quotes, with no luck. What am I missing here?

The sed command is executed under Windows/cygwin.


You're missing the fact that single quotes prevent variable substitution:

sed -i "s/$VAR some.file/\"$VAR\" some.file/g" path/to/file

or even

sed -i $(printf 's/%s some.file/"%s" some.file/g' "$VAR" "$VAR") path/to/file
0

精彩评论

暂无评论...
验证码 换一张
取 消