2

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

5
  • 1
    AFAIK many similar quirks in Windows appear because while programs in Linux work with arrays of arguments from the start, programs in Windows initially work with the entire command line as a string. In Linux your shell handles word splitting, quote removal and wildcards (like *) 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. Commented Jul 27, 2022 at 19:53
  • @SeñorCMasMas well, i'm on Windows 7, so, no WSL feature.. And yeah same grep as the other q, i've added a grep --version to this one. . Interestingly the command doesn't give that error when run from cygwin. . it's only when run from cmd that that happens. I just added a bit re under cygwin
    – barlop
    Commented Jul 27, 2022 at 20:31
  • Thank you for updating your question :) Commented Jul 27, 2022 at 20:31
  • 1
    When run from Cygwin.. I assume you mean bash. Hrrmm.. just for fun ;) ... what happens if you run cmd.exe from within your bash shell. Does it happen then? I use MingW64 when I have no WSL.. some bugs follow the console around that have nothing to do with bash. Commented Jul 27, 2022 at 20:34
  • @SeñorCMasMas then funny thing happen.. hitting up with the cursor won't go up the command history, it goes up the text of the cmd window, and I can then delete text from the cmd window. Even echo won't work correctly because quote can't be escaped! That's without even getting onto grep! Nice idea, I hadn't tried that before ever! pastebin.com/raw/Grm77UWX
    – barlop
    Commented Jul 27, 2022 at 20:44

0

You must log in to answer this question.

Browse other questions tagged .