burnthewhich.github.io

What’s wrong with whichcraft?

How to use “which” (very) badly

You often see old scripts with long boilerplate like this:

CAT=`which cat`
LS=`which ls`
TAR=`which tar`
TEST=`which test`

… going on ad nauseam for every single command that the script will later want to run. And then later on you’ll see $CAT instead of just cat.

This is by far the most common - and least justifiable - use of the which command in scripts.

The first clue is the use of `backticks` instead of $(parentheses). These show that’s you’re looking at old code. Or code written by someone who’s learned from ancient sources. Or the output of GNU AutoConf (which itself pre-dates POSIX).

There are additional the problems that are “co-effects” of the coding style, rather than directly caused by which:

Lastly, there’s the stylistic horror of storing code inside variables, which drastically reduces the efficacy of automated code analysis.