I am confused in bash by this expression:
$ var="" # empty var
$ test -f $var; echo $? # test if such file exists
0 # and this file exists, amazing!
$ test -f ""; echo $? # let's try doing it without var
1 # and all ok
I can’t understand such bash behaviour, maybe anybody can explain?
It’s because the empty expansion of
$varis removed beforetestsees it. You are actually runningtest -fand thus there’s only one arg totest, namely-f. According to POSIX, a single arg like-fis true because it is not empty.From POSIX test(1) specification:
There’s never a test for a file with an empty file name. Now with an explicit
test -f ""there are two args and-fis recognized as the operator for “test existence of path argument”.