Reputation: 173
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
Upvotes: 3
Views: 227
Reputation: 42999
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
.
Upvotes: 1
Reputation: 133508
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"
Upvotes: 1
Reputation: 531135
You didn't quote $x
, so your test becomes [ -f ]
, which is true because -f
is a non-empty string.
if [ -f "$x" ]; then
Upvotes: 5