Is it possible to set a variable from the output of a command inside of a conditional where the conditional is false if nothing gets assigned to the variable.
If I set the variable to a grep with no return and then test:
test=$(echo hello | grep 'helo')
if [[ ! -z $test ]]; then
echo "is set"
else
echo "not set"
fi
Output: not set (this is expected)
But I'm trying to put it all into one statement like this:
test=
if [[ ! -z test=$(echo hello | grep 'helo') ]]; then
echo "is set"
else
echo "not set"
fi
output: "is set" (expected not set)
test=is the same astest="", so the variable is not unset, but set to the empty string.test=is being compared against the empty string, not evaluated as an assignment.testdoesn't get assigned inside theifstatement. Instead,-zlooks at the string test=SOMETHING, and this string is always non-empty, because no matter what SOMETHING evaluates to, the argument of the-zoperator always starts with test=. Side note: If you replace! -zby-n, the code gets a bit more readable.