When I type the command

$ grep \\\\h junk  

then the shell should interpret \\\\h as \\h - as the two pairs of \\ each become \ . grep in turn, should interpret \\h as \h as \\ becomes \, so grep should search for the pattern \h in junk, which it is doing successfully.

But it's not working for \\\\$. Why?

  • better on superuser.com – YOU Apr 16 '10 at 11:14
  • Isn't $ some special symbol too? Try \\\$ – Draco Ater Apr 16 '10 at 11:34
  • 1
    @Draco Ater Unfortunately that's not enough, don't forget that the shell gets first bat against backslashed chars... which turns \\$ into \$, which grep then interprets as the EOL symbol. – David Apr 16 '10 at 11:36
  • @Happy: ok, i believe i see the difference. in this question and the other. reopening. please double-check that my edits are what you intended to say -- i've used markdown formatting for codeblocks (4-space-indent) and string literals (backticks), so that avoids having to type \\\\\\\\h just to see \\\\h. hope i got the numbers right. – quack quixote Apr 16 '10 at 11:53
  • Try using echo to see what input is going into grep. I.E., echo \\\$ will give output as \$. – Kevin Panko Apr 16 '10 at 15:42

To return lines with a dollar sign, another way without escapes is:

grep "[$]" file

To find lines with the literal \$:

grep "[\][$]" file
  • Very nice! Testing in bash, you don't need quotes either! – David Apr 16 '10 at 11:44

Returns only lines with the dollar sign in it.

grep "\\$" application/bootstrap.php

Because $ is a regex command as well ( end of line ), you have to quote it to get the double slashes past your shell and into grep.


Grep always uses \ to quote the next character, so \h simply becomes h in your example, since \h is not "special":

$ cat file

$ grep \\a file

$ grep \\\\a file
  • As in your last example, grep \\\\a searches for \a, shouldn't \\\\$ be searched for \$ because shell passes grep as \\$ and grep converts it to \$. am I correct? – Happy Mittal Apr 16 '10 at 11:44
  • As others have pointed out, $ matches the end of the string, so you have to quote it. If you want to search for the two characters '\$', then grep needs \\\$, and quoting for the shell gives you \\\\\\$ – Dan Andreatta Apr 16 '10 at 11:58

Assuming you're trying to match the character '$', the pattern:


is the easiest way to specify it without getting tangled up in multiple levels of character-escapes. Or if you're trying to match the two characters: '\$', then


will do it.


\h is not a special escape for grep, so '\h' should find a \h in a file, which it does. The shell removes the first \ before grep sees it, as \ is special (type ls \\ and it says ls: : So such file or directory ). So to match "\h" in a file, use grep \h, or '\h' to avoid the first \ --> \ reduction by the shell.

Now \$ : \ is reduced to \ by the shell, and grep sees \$ and because $ is special in regexes (like +,.,* etc), \$ is a literal dollar sign, as a simple test file will show you. See the man page for re_format (7). So this matches all lines with a dollar sign.

So if you want to match a literal "\$" in a file, we need to think: \$ is unescaped by the shell to literal $, and \ to : try ls \\\$' and you get "ls: \$: No such file or directory". So ingrep \\$ file, grep sees \$ and interprets that as $ again... So we want grep to see \\\$ (which it will unescape again to \$), so we can put single quotes around that and avoid headaches, or escape all the special characters for the shell too:grep \\\\$ file`. This works.

That's why I almost always put single quotes around the first grep argument, so I only have to think about what grep will do, and not about the shell as well.


Your shell is interpreting \\ as \, and $ as a first character of some control sequences, like variable names. Therefore \\\\ is interpreted by your shell as literal \\ (as you think), but $ needs to be escaped to be interpreted as literal character. Use \\\\\\\$ (seven backslashes) or '\\\$' (text in apostrophes is interpreted literally by shell).

  • sorry I mistakenly typed wrong question. Now I have edited it. Please explain answer. – Happy Mittal Apr 16 '10 at 11:38
  • But it's neither working with \\\\\$(five backslashes), nor with '\\$' – Happy Mittal Apr 16 '10 at 12:28
  • 2
    Please explain "not working" with a reproducible case. i.e. what contains the file you are "grepping", what command you run, what result you got you aren't expecting. – jlliagre Apr 16 '10 at 12:43

Why can't you just grep '\\$'? Single quotes prevent almost all interpolation.


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.