1

It seems that several commands like 'find' and 'which' (and surely others) are overridden by the git-bash versions if you launch powershell inside git-bash:

raw powershell (expected)

Which doesn't exist:

(base) PS C:\> which
which : The term 'which' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:1
+ which
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (which:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

'which find' does nothing:

(base) PS C:\> which find
which : The term 'which' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:1
+ which find
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (which:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Find runs windows version:

(base) PS C:\> find
FIND: Parameter format not correct
(base) PS C:\> find --help
FIND: Parameter format not correct

Powershell inside Git Bash (broken)

launch powershell:

user@PC0 MINGW64 ~
$ powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

Loading personal and system profiles took 614ms.

'which' exists and is git-bash version:

(base) PS C:\> which
Usage: /usr/bin/which [options] [--] COMMAND [...]
Write the full path of COMMAND(s) to standard output.

  --version, -[vV] Print version and exit successfully.
... (omitting the rest of the usage)

'which' confirmed as /usr/bin/ version:

(base) PS C:\> which which
/usr/bin/which

'find' confirmed as /usr/bin/ version via 'which':

(base) PS C:\> which find
/usr/bin/find 

'find' giving git-bash usage inside powershell:

(base) PS C:\> find --help
Usage: /usr/bin/find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...] [expression]

Default path is the current directory; default expression is -print.
Expression may consist of: operators, options, tests, and actions.
... (omitting the rest of the usage)

I am launching a powershell script by running it inside powershell.exe from my git-bash shell. It completely breaks the script because I use the windows version of 'find' but it ends up using the git-bash version of find which doesn't work the same.

Why is this happening?

How can I fix it?

Edit:

It seems PATH is inherited and being used by powershell, if I set PATH= then launch powershell via /c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe

It seems to fix it partially.

If I do that then commands like xcopy stop working, I guess they need powershell's default PATH.

I can grab my powershell path from $Env:Path and I can assign that to PATH before running powershell and it seems to fix everything.

This is not a dynamic solution though, how can I make it dynamic so it will work on anybody's PC without needing to know their default PATH?

0

1 Answer 1

1

Git Bash prepends several directories to the in-process value of $env:PATH, which gives precedence to the ported-to-Windows Unix utilities in case of naming conflicts.

  • In your case, find.exe refers to $env:ProgramFiles\Git\usr\bin\find.exe, which, as a ported version of the Unix find utility, works very differently from the standard Windows find.exe utility located in $env:WINDIR\System32

A simple way to give precedence to the regular Windows utilities is to prepend $env:WINDIR\System32 to $env:PATH:

  • That is, launch powershell.exe as follows from Git Bash:

    powershell.exe -NoExit -Command '$env:PATH="$env:WINDIR\System32;$env:PATH"'
    

Note that this still leaves the $env:ProgramFiles\Git-prefixed directories that Git Bash prepends in $env:PATH, preceding the original entries.
In other words: Executables located in directories other than $env:WINDIR\System32 could still be shadowed by Git Bash-supplied ones.
More work would be needed to categorically exclude them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.