Why escaped single quote doesn't work in grep?
6,536
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.
Related videos on Youtube
Author by
static
Updated on September 18, 2022Comments
-
static over 1 year
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?
-
barlop over 9 years-1 you really should've used a simpler example to demonstrate your point
-
-
Scott - Слава Україні almost 11 yearsTo 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…).
-
adam almost 8 yearsIf 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 proofecho "* * * * * /usr/local/bin/growlnotify -t 'helloTitle' -m 'helloMessage' -n 'myApp' -sw" | grep "^[^#].*/usr/local/bin/growlnotify.*-n 'myApp'.*$"