\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.