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.

you are viewing a single comment's thread
view the rest of the comments
[–] 8 points 1 year ago (2 children)

[ condition1 ] && [ condition2 ] && echo "Good" || echo "Bad"

  • source
  • hideshow 2 child comments
  • [–] 14 points 1 year ago*

    Explanation

    [ is an alias for the program test, so you can call man test for more info.
    && is bash syntax for conjunction. In A && B, B will only be called if A returned a exit code >0 (error). You can call man bash for more info.
    || is bash syntax for disjunction. In A || B, B will only be called if A returned exit code =0 (success). true and false are programs that just return exit codes 0 respectively 1.

  • source
  • parent
  • [–] 5 points 1 year ago* (last edited 1 year ago)

    Never use a && b || c. It is not the same as if a; then b; else c; fi: when a succeeds but b fails, it will run both b and c.

    I would not bother with [ unless you absolutely need compatibility with non-bash shells.

  • source
  • parent