42

I want to do a grep for \resources\. How do I do this?

I've tried:

grep \resources\
grep \\resources\\
grep "\resources\"
grep "\\resources\\"

None of these work.

0

3 Answers 3

46

The backslash is a special character for many applications:

  • including the shell: you need to escape it using another backslash or more elegantly, using single quotes when possible:

    $ printf '%s\n' foo\\bar 'foo\bar'
    foo\bar
    foo\bar
    

    Here the command received two arguments with value foo\bar, which were echoed as-is on the terminal.

    (Above, I used printf instead of echo as many echo implementations also do their own interpreting of backslash (here would expand \b into a backspace character)).

  • But backslash is also a special character for grep. This command recognizes many special sequences like \(, \|, \., and so on. So similarly you need to feed grep with a double \\ for an actual backslash character. This means that using the shell you need to type:

    grep 'foo\\bar'
    

    or equivalently:

    grep foo\\\\bar
    

    (both lines tell the shell to transmit foo\\bar as argument to grep).

  • Many other commands interpret backslashes in some of their arguments… and two levels of escaping are needed (one to escape the shell interpretation, one to escape the command interpretation).


By the way, for the shell, single quotes '…' prevent any kind of character interpretation, but double quotes only prevents some of them: in particular $, ` and \ remain active characters within "…".

33

You can also use fgrep (which is just grep with the -F flag). This forces grep to interpret the pattern as a fixed string (i.e. it'll treat a \ as a literal \). You'll still need to protect the backslashes from expansion by the shell.

grep -F '\resources\'
1
  • The flag is there for a reason! I was trying to grep for ..\ and even with '..\\' I didn't get the results I wanted. grep -F '..\' got me what I wanted! Thanks Dec 4, 2018 at 1:51
7

grep requires four backslashes to represent a backslash:

grep "\\\\resources\\\\"
3
  • 18
    This is a rather misleading oversimplification. grep only requires 1 backslash to escape another backslash, but the shell also requires backslashes to be quoted. Double quotes (") do NOT quote backslashes, but backslash does, which is why the double quotes in your example do nothing and the 4 backslashes are needed instead.
    – jw013
    Feb 9, 2012 at 18:45
  • 1
    I had to use four backslashes in Windows even though the Windows command-prompt does not treat backslashes like *nix does. I wonder if the GnuWin ports misunderstood the shell’s effect and hard-coded a 4-slash requirement, thinking it was what was required by grep.
    – Synetech
    Dec 25, 2015 at 19:09
  • Depending on the shell, the string in double quotes is interpreted and not taken literally. The result of this step is passed on to grep. When \\\\resources\\\\ is being interpreted, it will result in \\resources\\ and that is what is passed on to grep.
    – Hari
    Mar 15 at 11:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .