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)

3
  • 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. stackoverflow.com/a/59036879/45375 Commented Feb 24, 2023 at 2: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. Commented Feb 24, 2023 at 2: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. Commented Feb 24, 2023 at 4:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.