Home > Enterprise >  How to search for a string with one double quote with git grep in Windows cmd
How to search for a string with one double quote with git grep in Windows cmd

Time:02-11

The problem is that strings with two qoutes are searched well. But! I want to look for a string "Agent (one double quote!) inside my repo on cmd in Windows. So I having this:

git grep "\"Agent\"" > 1.txt (it works! all strings with "Agent" was found)

git grep "\"Agent" > 1.txt (error! fatal: ambiguous argument '>': unknown revision...)

Any ideas how to properly escape?

CodePudding user response:

You can use ^ to escape the " inside the terminal:

git grep ^""Agent" > 1.txt

If you search for a chunk of letters after the double quote, Agent, you do not even need to quote it:

git grep ^"Agent > 1.txt

will do.

CodePudding user response:

Interesting so here are my findings in CMD and Git Bash if you will use just one quote whether it is a single quote or double quote the command will break and bash will intercept it as 2 different commands. This is not only in the case of git grep but an echo will also have the same behavior.

Here you can see that I'm using syntax similar to yours in echo and it's not writing into the file just printing the next whole line

Now when I have added the closing quotes echo is working perfectly and writing into the file.

Now if you want to print a single quote you would have to use double quotes as starting and ending quotes and vice versa. Now according to this documentation, wrapping a single quote in double-quotes works and this works in the case of grep as well.

But its reverse (wrapping double quotes in single quotes) doesn't work in windows CMD this might be because bash is intercepting double quotes ("). But you can use git bash and below will work properly.

  • Related