[ condition1 ] && [ condition2 ] && echo "Good" || echo "Bad"
post
If you want true for empty strings, you want -z not -n
if [[ -z "VARIABLE1" && -z "VARIABLE2" ]]; then
echo "OK"
fi
To check for an empty string, use -z. -n checks to see if a string is not empty.
Thank you all for yours input
What finally did work
if [[ -z VARIABLE1 && -z VARIABLE2 ]]; then
echo "OK"
fi
If only Linux was using Python syntax that would be so much more intuitive...
You're probably wanting [ -z "${VAR1}" -a -z "${VAR2}" ]. Note in bash that there are minor differences in how [ ] and [[ ]] tests are handled. You can pull up a handy cheat sheet of the operands on most distros by running man test, though you'll need to read through the CONDITIONAL EXPRESSIONS section of man bash if you want to see the minor differences of the single vs double square bracket commands (mostly whether locale applies to string order, as well as whether operands are evaluated in numeric comparisons).
You need to reference the value of the variable, ie:
if [[ -n "$VARIABLE1" && -n "$VARIABLE2" ]]; then
echo "OK"
fi
not working, both variables do not exist and the echo "OK" do not trigger.
Then it is working. That is what that code was checking for.
Specifically, -n checks if the variable exists and also does not have a null value.
If you want to reverse it, ie, check that those conditions are not true, put an exclamation mark in front of the whole thing.
Try this:
#!/usr/bin/env bash
a=""
if [[ -z "${a}" && -z "${b}" ]]; then
echo "OK"
else
echo "Not OK"
fi
a="OK"
if [[ -n "${a}" && -z "${b}" ]]; then
echo "More ${a}"
else
echo "More Unokay"
fi
all 16 comments