Ishmael7
Ishmael7

Reputation: 173

Confusion about bash file exists test operator

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

Answers (3)

codeforester
codeforester

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.

Related:

Upvotes: 1

RavinderSingh13
RavinderSingh13

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

chepner
chepner

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

Related Questions