The following returns nothing:
which asdf
So why does the if statement get triggered here?
x=$(which asdf)
if [ -f $x ]; then echo "exists"; fi
You didn't quote $x, so your test becomes [ -f ], which is true because -f is a non-empty string.
if [ -f "$x" ]; then
Though Chepner has given good solution, in case you want to look for an alternate approach then try following once.
which asdf 2>&1 >/dev/null && echo "exists"
Looks like you are trying to check if a command exists. It is better to use the command builtin instead of which in that context, like this:
if command -v asdf; then
echo "exists"
fi
To learn more about command, try help command.