Home > database >  grep from MSYS2 not runnable from PowerShell Core function [duplicate]
grep from MSYS2 not runnable from PowerShell Core function [duplicate]

Time:09-29

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?

CodePudding user response:

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 }
  • Related