Cybercartel
Cybercartel

Reputation: 12592

How do I escape single quotes with perl interpreter?

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?

Upvotes: 8

Views: 15515

Answers (3)

ruakh
ruakh

Reputation: 183251

FatalError and gpojd have both given good solutions. I'll round this out with one other option:

find . | xargs perl -pi -e 's/\x27conflicts\x27 => \x27\x27,//g'

This works because in Perl, the s/.../.../ notation supports backslash-escapes. \x27 is a hexadecimal escape (' being U+0027).

Upvotes: 14

gpojd
gpojd

Reputation: 23055

Use double quotes around your code instead:

find . | xargs perl -pi -e "s/'conflicts' => '',//g"

Upvotes: 5

FatalError
FatalError

Reputation: 54551

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.

Upvotes: 11

Related Questions