I researched, and this is quite hairy:
-a
is deprecated, thus isn't listed in the manpage for /usr/bin/test
anymore, but still in the one for bash. Use -e
. For single '[', the bash builtin behaves the same as the test
bash builtin, which behaves the same as /usr/bin/[
and /usr/bin/test
(the one is a symlink to the other). Note the effect of -a
depends on its position: If it's at the start, it means file exists
. If it's in the middle of two expressions, it means logical and
.
[ ! -a /path ] && echo exists
doesn't work, as the bash manual points out that -a
is considered a binary operator there, and so the above isn't parsed as a negate -a ..
but as a if '!' and '/path' is true
(non-empty). Thus, your script always outputs "-a"
(which actually tests for files), and "! -a"
which actually is a binary and
here.
For [[
, -a
isn't used as a binary and
anymore (&&
is used there), so its unique purpose is to check for a file there (although being deprecated). So, negation actually does what you expect.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…