0

According to the documentation for which, it should "list all instances of executables" (-a) "in the user's path", and based on my understanding of what executables are and what a path is, that means, simply that the first listed item should be the one executed when I enter the name of the searched for command.

But that's not the behavior I'm seeing.

~ $ which -a speedtest 
/usr/local/bin/speedtest
~ $ speedtest -V
curl 7.64.1 (x86_64-apple-darwin20.0) ...
~ $ /usr/local/bin/speedtest -V
Speedtest by Ookla 1.0.0.2 (5ae238b) Darwin 20.4.0 x86_64

I expect that the speedtest returned first by which (Ookla's) would be the one that's found first and executed but it's not and a different one (curl's) is executed instead. Moreover, the speedtest that is excited isn't even retuned by which at all.

What's going on here? How is this even possible? Why isn't which -a listing the excitable that will run?


~ $ ls -al /usr/local/bin/speedtest
lrwxr-xr-x  1 Orome  admin  39 May 18 13:51 /usr/local/bin/speedtest -> ../Cellar/speedtest/1.0.0/bin/speedtest
~ $ /usr/local/bin/../Cellar/speedtest/1.0.0/bin/speedtest -V
Speedtest by Ookla 1.0.0.2 (5ae238b) Darwin 20.4.0 x86_64 ...
~ $ bash --version
GNU bash, version 5.1.8(1)-release (x86_64-apple-darwin20.3.0) ...
~ $ echo $PATH
/usr/local/opt/python@3.9/libexec/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin:/usr/local/sbin
~ $ whereis speedtest

orome
  • 35,904
  • 38
  • 156
  • 345
  • 3
    `which` is not a bash built-in. Use `type` instead. – oguz ismail May 23 '21 at 14:51
  • Maybe you forgot to run `hash -r` after an install or path update? – Roadowl May 23 '21 at 14:53
  • 2
    @oguzismail Aha! `which` doesn't list aliases! That's an answer. – orome May 23 '21 at 15:02
  • 1
    @orome `which` only searches the directories in `PATH` for executables; but when the shell goes to execute a command, it first looks for an alias by that name, then for a shell function, then for a shell builtin, and it's only if it doesn't find any of those that it searches `PATH`. In bash, `type` knows how to check for aliases, functions, and builtins as well as look at `PATH`. In zsh, `whence` is the command that knows how to check all the possibilities. – Gordon Davisson May 23 '21 at 16:13
  • Yes, @oguzismail already answered. That would be an accepted answer. – orome May 23 '21 at 21:37

0 Answers0