2

I'm coming from places like Why not use "which"? What to use then? and https://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script and am myself a long-time proponent of advising against the use of which for all the well-known reasons. But this is looking like an uphill battle.

Why is it so hard to get people to abandon the which command?

Many other POSIX innovations which were originally met with opposition and skepticism have over time become part of the normal U*x canon, but this seems to be one where adoption in the field is extremely spotty.

I'd like to understand why this specific POSIX recommendation is so hard for people to take to heart. Is it just that the POSIX stance has not been stable (first type, now command)? Or is it because the POSIX name command is somehow hard to remember or internalize? (I am wondering if Bash had command before POSIX did, perhaps with slightly different semantics?) Or is it still just the inundating mass of all the sites and forum posts which still recommend which, along with its undoubtedly slightly catchy name?

1
  • Maybe you should give people a reason to abandon the which command -- eg. make them believe that using which costs them 350M a week. – mosvy Aug 4 '19 at 23:56
1

Because command -v is not a direct replacement for which;

https://github.com/koalaman/shellcheck/issues/1162

which

  • outputs full path,
  • ignores shell built-ins and functions,
  • returns true only if all exist
  • outputs bin instead of alias
  • is available in dash unlike type -p
  • the overwhelming majority of computers use ~5 flaviours of Bash/Linux, so incompatible implementations are rare.
3
  • It's not like which would reliably provide any or all of these, either. Part of the rationale for replacing it with a different command was that there were many incompatible implementations, which is still true to this day. But thanks for the link! – tripleee Aug 3 '19 at 8:37
  • See the comment I added to the bug report just now: github.com/koalaman/shellcheck/issues/… (which redirects to the question I link in my question) but more fundamentally, I am not saying specifically that you should use command -v in every instance where you would otherwise use which – tripleee Aug 3 '19 at 8:43
  • 1
    In reaction to the latest edit: "Not caring about portability", while probably part of the true answer, is particularly non sequitur. Anyway, Bash does not have which, though the system where it is running might have one of the which commands installed. (But which one?) – tripleee Aug 3 '19 at 9:12
0

In addition to @user1133275's answer and related comments, one cannot always tell whether which or type is being used. Personally, my fingers are just trained to use which and I find that command name more mnemonically intuitive. But in fact, I am actually using type:

$ which which
$ which type
$ type type
type is a shell builtin
$ type which
which is a function
which () 
{ 
    type -path ${1+"$@"}
}

If tomorrow, someone comes up with a better command, I'll change my .profile and continue using my customized which.

In short, I think it's hard to eradicate because the name correlates well with its function.

2
  • Okay for habits which are hard to break. What aliases or functions you have defined for interactive use is something I would discount as private convenience. I have a lot of personal shorthands which however never leak into scripts I write or advice I share on the net: – tripleee Aug 3 '19 at 21:35
  • @tripleee Fair point. It's just hard to eradicate because it's a catchy name. – Jim L. Aug 3 '19 at 21:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.