\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 in
grep \\$ 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.
@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 – 2010-04-16T11:53:03.187better on superuser.com – YOU – 2010-04-16T11:14:47.673
Isn't
$
some special symbol too? Try\\\$
– None – 2010-04-16T11:34:01.0471@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 – 2010-04-16T11:36:25.753
Try using
echo
to see what input is going intogrep
. I.E.,echo \\\$
will give output as\$
. – Kevin Panko – 2010-04-16T15:42:20.130