Why do I have to escape a "dot" twice?
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.
Related videos on Youtube
Registered User
Updated on September 18, 2022Comments
-
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 almost 10 yearsCan you give the case that you have to escape twice?
-
Cthulhu almost 10 yearsMore precisely, you do not escape dot twice, you escape the escape character so that it gets passed to grep
-
Leonid Beschastny almost 10 yearsYou could use quotation marks to avoid escaping backslash characters:
man gcc | grep '\.'
. -
Izkata almost 10 yearsI strongly prefer @LeonidBeschastny's suggestion because of how much clearer it is what's going on
-
Stéphane Chazelas almost 3 yearsThat 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 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 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 almost 10 yearsI 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 almost 10 years@Gnouc I don't think it has.
echo .
in the bash just... echoes bach.
character. -
cuonglm almost 10 years@Cthulhu: Oh, my bad, updated.
-
cuonglm almost 10 years@Emmanuel: After Cthulhu's comment, see above.
-
Emmanuel almost 10 years@gnouc Ok sorry, I missed it
-
Stéphane Chazelas almost 3 yearsIn 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 usegrep -F .
. BTW, the OP made no mention of thebash
shell. Not all shells treat backslash or"..."
as quoting operators.rc
doesn't for instance (same comment applies to the accepted answer).