WHICH="`which which`"
is exactly equivalent to WHICH=`which which`
or to WHICH=$(which which)
or to WHICH="$(which which)"
. All of these run the command which which
, and capture its output (as a string) into the variable WHICH
, stripping off any final newline from the output. Similarly, AWK="`${WHICH} gawk`"
could be written AWK=`$WHICH gawk`
.
Normally, command substitutions (`command`
or $(command)
) should be surrounded by double quotes, in case the result contains whitespace or shell wildcards (*?[\
). This is because the result of command substitutions, like variable substitutions ($foo
or ${foo}
), undergoes word splitting and filename generation (a.k.a. globbing). However, this does not apply to simple variable assignments: since the context requires a single word, the result of the substitution is taken as-is. So assignments are an exception to the general rule that you should double-quote variable and command substitutions. (Note that this exception does not extend to export var="$value"
, which does require the double quotes.) But it's a reasonable shell programming practice to always the double quotes — omit them only if you know you must, rather than using them only if you know you must.
In the (unlikely) case which which
returns a path with spaces or wildcards, ${WHICH}
should be double-quoted in the lines below, i.e. one of
AWK="`"${WHICH}" gawk`"
AWK=`"${WHICH}" gawk`
AWK="$("${WHICH}" gawk)"
AWK=$("${WHICH}" gawk)
(Using $(…)
is recommended over `…`
, because of the difficulty of getting the nested quoting right inside `…`
. However very old shells don't recognize $(…)
, so `…`
is needed for scripts that must run on older systems.)
See also $VAR vs ${VAR} and to quote or not to quote
ECHO
is completely useless. But then all the calls towhich
here are useless, because all the script ever does is call the commands, which would look up thePATH
anyway. The quoting is also off: there are double quotes in the definitions of these variables, where they don't make a difference, but not when using the variables, where it would (and sometimes they're eveneval
ed). You mustn't call this script with any special character in a command name (or in$PASSWORD
, but using that is a bad idea anyway). – Gilles 'SO- stop being evil' Apr 19 '11 at 07:06