Why would the windows implementation of grep command here, and from cmd, give an error message of trailing backslash?
C:\Users\User>where grep
c:\cygwin\bin\grep.exe
C:\Users\User>c:\cygwin\bin\grep.exe --version
grep (GNU grep) 3.7
Packaged by Cygwin (3.7-2)
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and others; see
<https://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
C:\Users\User>
I see and know that this works
C:\>echo a^"b | grep ^"
a"b
C:\>
now looking at linux
user@comp:~# echo ab\"c
ab"c
user@comp:~# echo ab\"c| grep \"
ab"c
user@comp:~# echo ab\"c| grep \" <-- same command as above but with a space after the quote.
ab"c
user@comp:~#
Nothing strange so far.
Now looking at Windows though
C:\>echo a | grep \^"
grep: Trailing backslash
C:\>echo a | grep \^" <-- same command as above, but with a space after the quote.
a
This works
C:\>echo a^"b | grep ^"
a"b
C:\>
This gives an error about trailing backslash
C:\>echo a^"b| grep \^"
grep: Trailing backslash
C:\>
I don't understand why I get that error.
And this is related to that and also strange and I don't see why it's happening
If I put a space after that quote at the end then it doesn't give an error.
C:\>echo a^"b| grep \^"
C:\>
The above is looking for a quote followed by a space.
So if I do
C:\>echo a^"b | grep \^" <-- a space after that quote. And notive also a space before the pipe
a"b
That works no error.
So for some reason, I don't understand why. The windows implementation of grep, gives an error about trailing backslash, when looking for just a quote.
I can't see how to produce an equivalent "trailing backslash" error with grep on linux.
I can get an error about a trailing backslash on linux, but not with a quote at the end.
# echo a\\b | grep \\
grep: Trailing backslash
#
# echo a\\b | grep "\\"
grep: Trailing backslash
I can see that to find a single backslash in linux, I need four. 'cos the shell goes for the four turns them to two. Then grep takes the two and turns it to one literal backslash.
# echo a\\b | grep \\\\
a\b
# echo a\\b | grep "\\\\"
a\b
#
And the trailing backslash in linux, means there's a backslash on the end that has special meaning, because it's not escaped, and it's not doing anything.
# echo a\\b | grep \\
grep: Trailing backslash
#
As soon as I stick a literal quote on the end, no error
# echo a\\b | grep \\\"
#
#
So why, when I give the windows implementation of grep, something that doesn't end in a backslah, it'd give the error about a trailing backslash?
Note that if I run this from cygwin, I don't get any error about trailing backslash
$ echo a\"b | grep \"
a"b
From cygwin it behaves a lot like the linux one
so gives that trailing backslash message under the same circumstances as the linux one
$ echo a\\b | grep \\
grep: Trailing backslash
and escapes backslashes fine and has no problem with the literal quote at the end.
$ echo a\\b | grep \\\\
a\b
$
$ echo a\\b | grep \\\\\"
$
Also, i'm on Win7, so no WSL feature
*
) before a program starts. In Windows a program can be aware of any quote, trailing space or wildcard. It may use a standard function to convert the string to an array of arguments; or it may use whatever (compare this). I have a feeling this may play a role here.