bash sed 将 ' 替换为 \'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11514783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:26:05 来源:igfitidea点击:
sed replace ' with \'
提问by bguiz
This:
这个:
echo "Hel'lo" | sed s/\'/\\'/g
Yields
产量
Hel'lo
What I want is this:
我想要的是这个:
Hel\'lo
What am I missing?
我错过了什么?
回答by Seth Robertson
echo "Hel'lo" | sed "s/'/\\'/g"
Hel\'lo
Also
还
echo "Hel'lo" | sed s/\'/\\\'/g
回答by ghoti
You can also do it with all single quotes:
您也可以使用所有单引号进行操作:
echo "Hel'lo" | sed 's/'\''/\'\''/g'
And since your question is also tagged bash, I might as well point out that you don't even need sed:
而且由于您的问题也被标记为bash,我不妨指出您甚至不需要 sed:
[ghoti@pc ~]$ foo="Hel'lo"
[ghoti@pc ~]$ echo "${foo/\'/\'}"
Hel\'lo
回答by Adrian Pronk
Or without quoting the sed argument:
或者不引用 sed 参数:
echo "Hel'lo" | sed s/\'/\\\'/g
回答by potong
This might work for you:
这可能对你有用:
echo "Hel'lo" | sed 's/'\''/\&/'