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

21,410

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.

Share:
21,410

Related videos on Youtube

Registered User
Author by

Registered User

Updated on September 18, 2022

Comments

  • Registered User
    Registered User over 1 year

    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?

    • cuonglm
      cuonglm almost 10 years
      Can you give the case that you have to escape twice?
    • Cthulhu
      Cthulhu almost 10 years
      More precisely, you do not escape dot twice, you escape the escape character so that it gets passed to grep
    • Leonid Beschastny
      Leonid Beschastny almost 10 years
      You could use quotation marks to avoid escaping backslash characters: man gcc | grep '\.'.
    • Izkata
      Izkata almost 10 years
      I strongly prefer @LeonidBeschastny's suggestion because of how much clearer it is what's going on
    • Stéphane Chazelas
      Stéphane Chazelas almost 3 years
      That depends on what shell will be interpreting that code. See also How to use a special character as a normal one in Unix shells?
  • Thushi
    Thushi almost 10 years
    :So,Does the escape character for dot depends on the number of pipes we use?.For example cmd | cmd | cmd | cmd \\\\. Is that correct????
  • tiwari.vikash
    tiwari.vikash almost 10 years
    @Thushi: No. This has nothing to do with the fact that you are using a (or several) pipe characters, but applies even for grep \\. my_file. The commandline is interpreted by the shell, using the first \ to escape the second one, so one \ is passed literally to grep. The dot . is not special to the shell, so it is passed verbatim anyway. Grep then reads the (single) \ and uses it to escape the dot ..
  • Cthulhu
    Cthulhu almost 10 years
    I believe the answer is somewhat incorrect in that it says "The first escape causes bash knows . is literal, the second is for grep.". Actually, the first escape lets bash know that \ is leteral, and pass \. to grep.
  • Cthulhu
    Cthulhu almost 10 years
    @Gnouc I don't think it has. echo . in the bash just... echoes bach . character.
  • cuonglm
    cuonglm almost 10 years
    @Cthulhu: Oh, my bad, updated.
  • cuonglm
    cuonglm almost 10 years
    @Emmanuel: After Cthulhu's comment, see above.
  • Emmanuel
    Emmanuel almost 10 years
    @gnouc Ok sorry, I missed it
  • Stéphane Chazelas
    Stéphane Chazelas almost 3 years
    In general, it would be better to use single quotes as backslash is still special within double quotes (though not when followed by .) in Bourne-like shells. Or use grep -F .. BTW, the OP made no mention of the bash shell. Not all shells treat backslash or "..." as quoting operators. rc doesn't for instance (same comment applies to the accepted answer).