How to run the "other" executable matching the same name found in PATH?

0

Is there a concise way to call the "other" executable for find available in path that can be found with where?

C:\repos>where find
C:\Windows\System32\find.exe
C:\Program Files\Git\usr\bin\find.exe
C:\Users\Qwerty\cmd\UnxUtils_wbin\find.exe

For example something like find~2 or wannabe whereget find -n 2 -- <arguments for find here>

Qwerty

Posted 2019-04-10T12:53:42.267

Reputation: 308

Answers

1

Calling an executable without the full path will always find and use the first matching filename by searching the directories in $PATH. For your example, if you always want to use the git find, simply ensure that the path to it appears in your $PATH before the Windows find. If this is unsatisfactory or impossible (e.g., due to policy), you should either create an alias in bash for it, or always call it using the full pathname.

Jeff Zeitlin

Posted 2019-04-10T12:53:42.267

Reputation: 2 918

0

I will just put it here

$ alias whereget='_whereget() { A=$1; B=$2; shift 2; eval \"$(where $B | head -$A | tail -1)\" $@; }; _whereget'
$ whereget 2 find . -type d

Unfortunately this solution only works in bash, not in Windows cmd.

Qwerty

Posted 2019-04-10T12:53:42.267

Reputation: 308

The alias won't work either in a bash script. – xenoid – 2019-04-10T14:22:45.323

Interesting, it only works in the active console. Would you mind clarifying why? @xenoid – Qwerty – 2019-04-11T07:41:08.570

1Because when you run a script, it's a new bash instance in which the alias is not defined (only properly exported environment variables are inherited from the parent). If your "source" the file, it is run in the same bash instance, and the alias is used. And I don't see a way to define the alias for a script, since on the interactive bash instances read the profile files (.bashrc and .bash_profile). – xenoid – 2019-04-11T09:58:18.880

Right, then I will put it to ~/.bashrc file – Qwerty – 2019-04-11T11:21:32.410

won't work, because, as I said .bashrc is only used by interactive shells. It is not run by instances of bash that are started to run scripts. And you can't use functions for the same reason. – xenoid – 2019-04-11T11:48:32.787