I want to do a grep for
\resources\. How do I do this?
grep \resources\ grep \\resources\\ grep "\resources\" grep "\\resources\\"
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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:
(both lines tell the shell to transmit
foo\\bar as argument to
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
\ remain active characters within
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\'
grep requires four backslashes to represent a backslash: