top | item 34850255

(no title)

gwillen | 3 years ago

Beware -- command -v does not have the same semantics as which, and is not a substitute for it in shell scripts. There is no direct substitute. See: https://github.com/koalaman/shellcheck/issues/1162

discuss

order

metadat|3 years ago

For most purposes, the difference is pretty nuanced and unlikely to cause a problem in real life.

> 'command -v' also returns 0 for shell built-ins, whereas 'which' only searches $PATH.

In what cases is one using anything to check for a built-in shell command? I've written quite a lot of bash, and this has never come up.

I've learned to prefer 'command -v' because it's always available, whereas on minimal or busybox installs sometimes 'which' is not included.

The annoying part seems to be more that the ergonomics of a single straightforwardly-named command (e.g. 'which') are friendlier and easier to remember compared to an unintuitively-named command requiring the addition of a flag.

Why can't we move in a more ergonomic direction by promoting 'which' to a BASH built-in?