Why do I have to escape a "dot" twice?

I know that we can escape a special character like *(){}$ with so as to be considered literals.
For example * or $

But in case of . I have to do it twice, like \. otherwise it is considered special character. Example:

man gcc | grep \.

Why is it so?

Asked By: Registered User


Generally, you only have to escape one time to make special character considered literal. Sometime you have to do it twice, because your pattern is used by more than one program.

Let’s discuss your example:

man gcc | grep \.

This command is interpreted by two programs, the bash interpreter and grep. The first escape causes bash to know is literal, so the second is passed for grep.

If you escape only one time, ., bash will know this dot is literal, and pass . to grep. When grep see this ., it thinks the dot is special character, not literal.

If you escape twice, bash will pass the pattern . to grep. Now grep knows that it is a literal dot.

Answered By: cuonglm

To prevent bash from trying to interpret your arguments, enclose them in quotes

man gcc | grep "."
Answered By: Rucent88