Search code examples
bashif-statementboolean-operations

bash compound if statement with booleans


I am trying to figure out why bash "miss-behaves":

REG=true 
VIN=false

if $REG -a $VIN; then
    echo $REG $VIN
fi

when run, it yields:

$ bash test.sh 
true false

I was expecting nothing, can you show how to make a complete evaluation of both variables ?


Solution

  • Use && instead in the most POSIX way:

    if [[ "$REG" && "$VIN" ]]; then
        echo $REG $VIN
    fi
    

    If you want to use the -a, then surround with brackets:

    if [ "$REG" -a "$VIN" ]; then
    

    if $REG && $VIN; then