Using "false" in if statement KSH

My code:

if $bVar1 && $bVar2
then
    echo "bVar1 & bVar2 are true"
elif $bVar3 && !$bVar4
then
    echo "bVar3 is true; bVar4 is false"
fi

The !$bVar4 part does not work as expected. I've tried:

elif $bVar3 && !$bVar4
elif $bVar3 && !${bVar4}
elif $bVar3 && !$(bVar4)

but I cannot get this line to return true when bVar3=true and bVar4=false.

For completeness, variables are assigned like this:

bVar3=true
bVar4=false

(Of course, I can add a nested if statement within the elif like this:

if $bVar1 && $bVar2
then
    echo "bVar1 & bVar2 are true"
elif $bVar3
then
    if $bVar4
    then
        : #pass
    else
        echo "bVar3 is true; bVar4 is false"
    fi
fi

but that is unnecessary, isn't it? BTW - I did try this and this code works.)

Upvotes: 0

Views: 1337

Answers (1)

William Pursell
William Pursell

Reputation: 212346

Shells are finicky with respect to whitespace. For example, in an interactive bash shell with certain settings, !$bVar4 will attempt to do history expansion as a result of the !, but ! $bvar4 will expand the string $bvar4, attempt to execute the resulting string, and then negate the return value. Adding a space after the ! is probably necessary to ensure the expected semantics.

Upvotes: 2

Related Questions