Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
    
-1 you really should've used a simpler example to demonstrate your point – barlop Nov 22 '14 at 19:30

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
    
If you are using GNU grep, you could have also replaced the single quotes ' enclosing the expression with double quotes " (2 chars modified) and removing the backslashes \ (3 chars removed). Copy/paste proof echo "* * * * * /usr/local/bin/growlnotify -t 'helloTitle' -m 'helloMessage' -n 'myApp' -sw" | grep "^[^#].*/usr/local/bin/growlnotify.*-n 'myApp'.*$" – adam Jun 18 at 21:43

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 '\'' <-- doesn't work

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.