You happened to have which
defined as:
which(){
/usr/bin/which -a "$@" |
xargs ls -l |
tr -s ' ' |
cut -d ' ' -f 9-
}
ls
sorts the filename lexically on output and /usr/local/bin/git
comes after /usr/bin/git
because l
comes after b
in your locale.
The GNU implementation of ls
has a -U
option to disable that sorting.
Your /usr/bin/which
command seems to be one that prints the path of all command names found in $PATH
when passed a -a
option. With zsh
builtins, you can do the same with whence -pa
¹.
So you could do something like:
mywhich() (
set -o pipefail
zmodload zsh/stat
whence -pa "$@" |
while IFS= read -r f; do
if [[ -L $f ]] && stat -A l +link -- $f; then
print -r -- "$f -> $l"
else
print -r -- $f
fi
done
)
(here assuming that none of the file paths contain newline characters).
A more correct version of yours, on a GNU system, would be like:
mywhich() (
set -o pipefail
command which -a "$@" |
xargs -rd '\n' ls -ndU -- |
sed -E 's/([^ ]+ +){8}//'
)
In any case, note that bash
doesn't have a which
builtin, so what which
outputs there is independent of the shell. Only tcsh
and zsh
have which
builtin.
¹ though, like your /usr/bin/which
(but contrary to zsh's builtin which
), it won't necessarily tell you which command the shell would run as it ignores aliases, functions, builtins and even the $hash
table of executables. See also Why not use "which"? What to use then?
bash
, you get the externalwhich
utility. Fromzsh
, you get the built-inwhich
, which is the same aswhence -c
. Fromzsh
, you could try/usr/bin/which
instead. You haven't shown what yourPATH
is in each shell, only that it's something unusually short in one of them (uncertain which one). What is the issue that you're trying to solve where the ordering of these strings is important? – Kusalananda Oct 23 '21 at 08:38which
to determine which executable will be executed. – Michael Ozeryansky Oct 23 '21 at 08:59type which
inzsh
? Thatgit -> ...
is not something zsh's builtinwhich
command would ever output AFAIK. You might have some form of custom alias for which. – Stéphane Chazelas Oct 23 '21 at 09:23which(){ /usr/bin/which -a "$@" | xargs ls -l | tr -s ' ' | cut -d ' ' -f 9- }
I'll close the question. – Michael Ozeryansky Oct 23 '21 at 09:33