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.

  • kittenroar@beehaw.org
    link
    fedilink
    English
    arrow-up
    3
    ·
    19 天前

    You need to reference the value of the variable, ie:

    if [[ -n "$VARIABLE1" && -n "$VARIABLE2" ]]; then
        echo "OK"
    fi
    
    • Rick_C137OP
      link
      fedilink
      arrow-up
      2
      ·
      19 天前

      not working, both variables do not exist and the echo "OK" do not trigger.

      • kittenroar@beehaw.org
        link
        fedilink
        English
        arrow-up
        2
        ·
        19 天前

        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.