I don't quite understand this behaviour of bash test operator. I used this line to determine if vagrant is installed.
if [ $(vagrant --version > /dev/null) ]; then echo "HELLO"; fi
I now know that this returns false (1) and that you should type only
if vagrant --version > /dev/null; then echo "HELLO"; fi
to get the expected result. But I still don't understand why [ $(vagrant --version > /dev/null) ] returns false (1). Shouldn't it return true (0) since
$(vagrant --version > /dev/null)
returns true, which is an expression (isn't it?).
From test man page:
The test utility evaluates the expression and, if it evaluates to true, returns a zero (true) exit status; otherwise it returns 1 (false). If there is no expression, test also returns 1 (false).
$(vagrant --version > /dev/null)
produce no output in case of success.test
with empty string return false as defined by POSIX.$(vagrant --version > /dev/null)
is not an expression, it is simply a subcommand. Test expressions are: File type and characteristic tests, access permission tests, string tests, or numeric tests, and connectives !, -a, and -o. Test doesn't return the status of an internal command, it returns a status depending on the evaluation of an expression.if vagrant --version > /dev/null 2>&1; then echo "HELLO"; fi