Hi,
I trying to test two condition together (AND) under bash but it’s not working…
The goal is ti have True when two variables are either not set or empty (empty string)
I’ve tried
if [[ -n VARIABLE1 && -n VARIABLE2 ]]; then
echo "OK"
fi
Here I get the “OK” no matter what .
Thanks.
[ condition1 ] && [ condition2 ] && echo "Good" || echo "Bad"
Never use
a && b || c
. It is not the same asif a; then b; else c; fi
: whena
succeeds butb
fails, it will run bothb
andc
.I would not bother with
[
unless you absolutely need compatibility with non-bash shells.
To check for an empty string, use
-z
.-n
checks to see if a string is not empty.in
[[
, empty strings are falsy, so this also works:[[ ! $VARIABLE1 && ! $VARIABLE2 ]] && echo "OK"
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…
The variables need a dollar sign:
$VARIABLE1
help test
shows what-n
and-z
do.
Could try:
if [ condition1 ] && [ condition2 ]; then echo "OK" fi