Depends upon the system. In my system, it does write to stderr:
$ which abcdasdf 1>/dev/null
which: no abcdasdf in (/usr/lib/mpi/gcc/openmpi/bin:/usr/local/bin:/usr/bin:/bin:.....snip)
$ uname -srv
Linux 3.4.47-2.38-desktop #1 SMP PREEMPT Fri May 31 20:17:40 UTC 2013 (3961086)
I'm intrigued by your uname command. What kind of information adds to this case? Honest question, I would have expected the following command to provide context:
$ which --version | head -1
GNU which v2.20, Copyright (C) 1999 - 2008 Carlo Wood.
Or may be the output of lsb_release, although it may not be installed in your system.
Is "which" behaviour dependant on the kernel version?
EDIT: formatting; also to add that which writes to stderr if the command you're asking for can't be found in the path.
function debug()
{
if [ -v VERBOSE ]
then
echo "${1}" # might want to add: >2
fi
}
function has_dep()
{
unset dependency
dependency=$(command -v "${1}")
if [ -v dependency ]
then
debug "found \"${1}\": ${dependency}"
return 0
else
debug "\"${1}\": not found"
return 1
fi
}
# example:
VERBOSE=1
if has_dep curl
then
echo "we're good"
fi
if has_dep notfoundthing
then
echo "not good."
fi
Betty:srcs lelf$ perl -E 'say "out"; say STDERR "err";' &> out && cat out
err
out
Betty:srcs lelf$
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)
Here's what is done there:
(`2>&1 > /dev/null` drops stdout and writes stderr to stdout—not what was meant. `which` doesn't write to stderr, so I drop that part.)That can be shortened significantly by using the return code directly in the if branch:
Or by just using the return code directly: And as it will return the return code of the last statement executed: