1

Say I am wanting to grep the raw string ["data-foo"]

Under git bash, the following returns the expected results

git grep --fixed-strings '["data-foo"]'

However, under powershell, I cannot get any results with any of these variants:

git grep --fixed-strings '["data-foo"]';
git grep --fixed-strings "[""data-foo""]";
$s = '["data-foo"]'; git grep --fixed-strings $s;
git grep --fixed-strings '\\["data-foo"\\]';
git grep --fixed-strings '\["data-foo"\]';

My understanding is that --fixed-strings should prevent git from attempting to parse the argument, and using single-quotes in powershell strings should prevent powershell string interpolation.

That leaves me to guess that somehow powershell is still parsing the string argument before actually handing it to the git binary, but I've been unable to determine why it would or how to properly "escape" the argument, since the raw string (e.g. $s) is properly echoed on the powershell console.

(Last tested with git version 2.39.2.windows.1)

Nathan
  • 10,593
  • 10
  • 63
  • 87
  • 1
    The sad reality up to PowerShell 7.2.x is that an _extra, manual_ layer of ``\``-escaping of embedded `"` characters is required in arguments passed to _external programs_. This has been fixed in PowerShell 7.3.0, with selective exceptions on Windows, for now _by default_ - though a future version may revert to the old, broken behavior, requiring _opt-in_ for the fix. See the linked duplicate for details. https://stackoverflow.com/a/59036879/45375 – mklement0 Feb 24 '23 at 02:16
  • 1
    In short: Use `git grep --fixed-strings '[\"data-foo\"]'`in PowerShell up to v7.2.x. In v7.3+, you may have to set `$PSNativeCommandArgumentPassing = 'Legacy'` for this to work, or you can set `$PSNativeCommandArgumentPassing = 'Standard'` and use `git grep --fixed-strings '["data-foo"]'` as-is. – mklement0 Feb 24 '23 at 02:18
  • 1
    @mklement0 Thanks for the link to the other question.. My `$PSVersionTable` shows I'm running on powershell 5.1. I mistakenly thought the square brackets were the problem because when I removed the brackets I got results - but I didn't notice the `"` weren't actually part of the pattern results matched by git under powershell. – Nathan Feb 24 '23 at 04:47

0 Answers0