Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to match a string:

* * * * * /usr/local/bin/growlnotify -t 'helloTitle' -m 'helloMessage' -n 'myApp' -sw

with:

crontab -l | grep '^[^#].*/usr/local/bin/growlnotify.*\-n \'myApp\'.*$'

it doesn't work: nothing matched.

But:

crontab -l | grep '^[^#].*/usr/local/bin/growlnotify.*\-n.*$'

works very good:

* * * * * /usr/local/bin/growlnotify -t 'helloTitle' -m 'helloMessage' -n 'myApp' -sw

What is the problem with \'myApp\' ?

How to escape a single quote in grep/sed?

share|improve this question

2 Answers 2

one can escape single quote with -e option and using hexadecimal representation:

echo -e '\047'
> '

or in grep using hexadecimal representation and -P option:

crontab -l | grep -P '^.*/usr/local/bin/growlnotify.*\-n \047myApp\047.*$'
> * * * * * /usr/local/bin/growlnotify -t 'helloTitle' -m 'helloMessage' -n 'myApp' -sw

ss64 as reference:

-P
   --perl-regexp
       Interpret PATTERN as a Perl regular expression.
share|improve this answer

Sorry to say, but you cannot escape single quotes inside single quotes. You'll need to use double quotes. You can prove it to yourself by doing:

echo '\''

Your shell will expect another quote.

share|improve this answer
2  
To clarify: "\" cannot be used to escape anything inside single quotes. This is not a grep/sed issue; this is a shell issue (bash, csh, etc…). –  Scott May 20 '13 at 22:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.