5

I would like to search \G string including the backslash with grep.

$ echo "\G\\G" > /tmp/test
$ grep '\G' /tmp/test 
\G\G
$ grep '\\G' /tmp/test 
\G\G
$ grep "\\G" /tmp/test 
\G\G
$ grep "\G" /tmp/test 
\G\G

Also see the screenshot below for the matches in red:

enter image description here

I was wondering why only '\\G' works? Is it because of bash only, or because of both bash and grep? Thanks.

  • 1
    Here's a fun one: echo '\G' | eval grep \\\\\\\\G – Fox Jan 16 '18 at 4:30
7

This has to do with what grep sees versus what the shell sees. So, both.

If you want to see what grep sees with various forms of quoting, use, for example:

printf '<%s>\n' G "G" 'G' \G "\G" '\G' \\G "\\G" '\\G'

This will demonstrate what the shell does with the various types of quoting.


If grep sees just a G, it will search for (and highlight, with your settings) just the G matches.

If grep sees a single backslash followed by a G, it will (in your implementation and probably all current implementations) consider that the backslash removes any special meaning from the character G. But there isn't any special meaning to G, so the result will be the same as if you just pass G.

If grep sees two backslashes, the first removes the special meaning from the second. So when grep sees \\G, it searches for a literal backslash followed by a G. That's what you want.

If you use the -F flag to grep, to search for a fixed string, you can pass just \G and get the same result. (That is, if you pass \G so that grep sees \G, which will require that you escape the backslash in some way so the shell doesn't remove it.)

| improve this answer | |
  • that printf command is very illuminating, it taught me a lot about my shell. Thanks for sharing! – GMA Aug 14 '18 at 8:45
  • But why grep "\\G" /tmp/test (with double quotes) didn't work. – Leonardo Castro Dec 13 '18 at 19:31
  • @LeonardoCastro in double quotes, the shell interprets backslashes. So when you type "\\G" into the shell, what the grep command sees is just \G - a single backslash followed by a G. I already covered in my answer what it will do in that case. – Wildcard Dec 13 '18 at 22:04
1

In Mentioned example you have mentioned \G\\G

\\G==> will consisdered as \G only .Back slash will make it treat as normal string instead of special meaning in linux. So first back slash will make next coming string to treat as normal string. So first back slash will be not be consisdered

if You want \\G as normal string then you need to mention as echo "\G\\\G"

i tested by using below example

created one file by mentioning below content

cat test.txt

\G
\\G

If i want to grep only \G i will be using below command

grep '^\\G' test.txt

output

\G

If i want to grep only "\\G" i will be using below command

grep '\\\\G' test.txt

output

\\G

Instead of this we can do this by sed command

For find line which contains \G using below command

sed -n '/^\\G/p' test.txt

For find line which contains \\G using below command

sed -n '/\\\\G/p' test.txt
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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