初衷是想把变量的双引号替换为带斜杠的转义的双引号并输出。

示例代码如下:

#! /bin/bash
test=\"sdfdsfds\"123
echo $test

test=$(echo $test | sed 's/\"/\\\"/g')
echo $test

输出结果:

"sdfdsfds"123
\"sdfdsfds\"123

参考链接:https://www.experts-exchange.com/questions/27573650/Replace-double-quotes-and-single-quotes-with-backslash-in-string-using-sed.html

Related Posts: Linux Shell里变量如何替换双引号 :

avatar