0

I'm trying to setup grep installed with Git Bash for Windows to be available from PowerShell Core.

If I have the following in my Microsoft.PowerShell_profile.ps1:

Set-alias grep "C:\Program Files\Git\usr\bin\grep.exe"

it works - I can use it with the pipes:

❯ echo abc | grep a
abc

But I'm missing the --color=auto option that on Linux is usually configured in .bashrc.

With some research, I found that PowerShell doesn't support aliases with parameters. The workaround is to write a function.

So I did it:

Set-alias _grep "C:\Program Files\Git\usr\bin\grep.exe"
function grep {_grep --color=auto $args}

But it doesn't work. The function configured this way doesn't return any output and freezes the shell.

If I manually provide --color=auto to grep used in the pipe, it works fine (output is returned and a is highlighted):

❯ echo abc | _grep --color=auto a
abc

ls --color=auto used in a function like in the link I provided above also works fine, so I guess that the problem is in my function, which is incompatible with the pipe.

Unfortunately, I don't know PowerShell too well, any ideas?

tpwo
  • 484
  • 3
  • 12
  • A slight caveat re the `$input | ...` approach: all pipeline input is _collected in full, up front_, before it is passed on to the external target program. – mklement0 Sep 28 '21 at 21:11

1 Answers1

3

You need to redirect the piped input to the command you're wrapping.

The most straightforward way is via the $input automatic variable:

function grep { $input | _grep --color=auto @args }
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206