Coder Perfect

I got “Trailing Backslash” when I used grep “” XXFile.

Problem

Now I want to see if there are any lines that include the ” character. I used grep “” XXFile, but it only returned “Trailing Backslash.” However, when I used grep ” XXFile, everything was OK. Could someone perhaps clarify why the first case isn’t working? Thanks.

Asked by dong

Solution #1

The distinction is in the way the shell handles backslashes:

If you’re still not sure, we can use echo to observe what the shell is doing. Because echo does not do any backslash interpretation, the output is exactly what the shell gave to it.

$ echo "\\"
\
$ echo '\\'
\\

Answered by John Kugelman

Solution #2

The command might have been worded as

grep "\\\\" ...

This has two backslashes that bash will split into two single backslashes. This new pair will be provided to grep as an escaped backslash, which will give you the results you want.

Answered by Nikolaos Kakouros

Post is based on https://stackoverflow.com/questions/20342464/when-grep-xxfile-i-got-trailing-backslash