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.

    • Farnsworth
      link
      fedilink
      arrow-up
      3
      ·
      11 months ago

      in [[ there is generally no need for quoting, except the right hand side of an = comparison

  • DeuxChevaux
    link
    fedilink
    arrow-up
    7
    ·
    11 months ago

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

    • suff@piefed.social
      link
      fedilink
      English
      arrow-up
      14
      ·
      edit-2
      11 months 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.
    • Farnsworth
      link
      fedilink
      arrow-up
      5
      ·
      edit-2
      11 months 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.

    • Farnsworth
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      11 months ago

      in [[, empty strings are falsy, so this also works:

      [[ ! $VARIABLE1 && ! $VARIABLE2 ]] && echo "OK"
      
  • Rick_C137@programming.devOP
    link
    fedilink
    arrow-up
    3
    ·
    11 months ago

    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…

    • Farnsworth
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      11 months ago

      The variables need a dollar sign: $VARIABLE1

      help test shows what -n and -z do.

  • Dion Starfire@sh.itjust.works
    link
    fedilink
    arrow-up
    3
    ·
    11 months ago

    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).

  • thingsiplay@beehaw.org
    link
    fedilink
    arrow-up
    2
    ·
    11 months ago

    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
    
  • fubarx
    link
    fedilink
    arrow-up
    2
    arrow-down
    1
    ·
    11 months ago

    Could try:

    if [ condition1 ] && [ condition2 ]; then
      echo "OK"
    fi