bash 如何使用 perl 解释器转义单引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16700565/
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
How do I escape single quotes with perl interpreter?
提问by Gigamegs
How do I escape the single qoutes in my bash expression find . | xargs perl -pi -e 's/'conflicts' => '',//g'? I want to replace the string 'conflicts' => '', in my files?
如何在 bash 表达式中转义单个 qoutes find . | xargs perl -pi -e 's/'conflicts' => '',//g'?我想在我的文件中替换字符串 'conflicts' => ''?
回答by ruakh
FatalError and gpojd have both given good solutions. I'll round this out with one other option:
FatalError 和 gpojd 都给出了很好的解决方案。我会用另一种选择来解决这个问题:
find . | xargs perl -pi -e 's/\x27conflicts\x27 => \x27\x27,//g'
This works because in Perl, the s/.../.../notation supports backslash-escapes. \x27is a hexadecimal escape ('being U+0027).
这是有效的,因为在 Perl 中,该s/.../.../符号支持反斜杠转义。\x27是一个十六进制转义'符(即U+0027)。
回答by FatalError
You can't directly escape it within single quotes, so to get a single quote you need to do something like:
您不能直接在单引号内转义它,因此要获得单引号,您需要执行以下操作:
$ echo 'i'\''m a string with a single quote'
i'm a string with a single quote
This ends the quoted part, escapes a single quote as it would appear outside of quotes, and then begins the quoting again. The result will still be one argument.
这将结束引用部分,转义单引号,因为它会出现在引号之外,然后再次开始引用。结果仍然是一个参数。
回答by gpojd
Use double quotes around your code instead:
改为在代码周围使用双引号:
find . | xargs perl -pi -e "s/'conflicts' => '',//g"

