Skip to content

/usr/bin/which -> type -P#3944

Merged
MikeMcQuaid merged 1 commit intoHomebrew:masterfrom
bukzor:command-not-which
Mar 19, 2018
Merged

/usr/bin/which -> type -P#3944
MikeMcQuaid merged 1 commit intoHomebrew:masterfrom
bukzor:command-not-which

Conversation

@bukzor
Copy link
Contributor

@bukzor bukzor commented Mar 18, 2018

This allows odd POSIXen (i.e. Android) to (at least attempt to) function.

Ported from Linuxbrew/brew#621

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the same behaviour as which -a; it produces a single result rather than multiple.

@bukzor bukzor changed the title command is more portable than which /usr/bin/which -> which Mar 18, 2018
@bukzor bukzor force-pushed the command-not-which branch from b5de197 to a397ea1 Compare March 18, 2018 19:52
@sjackman
Copy link
Contributor

sjackman commented Mar 18, 2018

I suggest using the bash builtin type -ap rather than the external command which -a. This script is #!/bin/bash.

❯❯❯ type type which
type is a shell builtin
which is hashed (/usr/bin/which)
❯❯❯ which -a git
/usr/local/bin/git
/usr/bin/git
❯❯❯ type -ap git
/usr/local/bin/git
/usr/bin/git

@MikeMcQuaid
Copy link
Member

I suggest using the bash builtin type -ap rather than the external command which -a. This script is #!/bin/bash.

Works for me. There may be other locations where that would be useful/needed too.

@bukzor
Copy link
Contributor Author

bukzor commented Mar 18, 2018

Do you like bash-isms in this project? For example, dash doesn't like type -ap.

@MikeMcQuaid
Copy link
Member

Do you like bash-isms in this project?

Yup, we use a bunch and we require /bin/bash 👍

@bukzor bukzor force-pushed the command-not-which branch from a397ea1 to c259d2f Compare March 18, 2018 23:29
@bukzor bukzor force-pushed the command-not-which branch from c259d2f to 0a2e649 Compare March 18, 2018 23:52
@bukzor bukzor changed the title /usr/bin/which -> which /usr/bin/which -> type -P Mar 18, 2018
@maxim-belkin
Copy link
Contributor

To be absolutely sure that you're using a builtin, a not /usr/bin/type alter-ego, prefix the command with builtin: builtin type -ap :)

@MikeMcQuaid MikeMcQuaid merged commit 08db242 into Homebrew:master Mar 19, 2018
@MikeMcQuaid
Copy link
Member

Thanks again @bukzor!

@bukzor
Copy link
Contributor Author

bukzor commented Mar 19, 2018

@maxim-belkin Is there any case where $PATH will override a built-in? I was pretty sure that builtin is only useful for avoiding a function or alias of the same name.

Also, I can't find any mention of /usr/bin/type. What is it?

@maxim-belkin
Copy link
Contributor

builtins can be disabled with enable -n but otherwise - you're correct.
On my mac, there is a bunch of /usr/bin/<filename> with the following contents:

#!/bin/sh
# $FreeBSD: src/usr.bin/alias/generic.sh,v 1.2 2005/10/24 22:32:19 cperciva Exp $
# This file is in the public domain.
builtin `echo ${0##*/} | tr \[:upper:] \[:lower:]` ${1+"$@"}

and type is one of them...
See https://stackoverflow.com/a/38809607

@MikeMcQuaid
Copy link
Member

I'm happy with this as-is. We can investigate further if we hit any bugs.

@bukzor bukzor deleted the command-not-which branch March 20, 2018 17:18
@Homebrew Homebrew locked and limited conversation to collaborators May 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants