Using "and" in Bash while loop

The [] operator in bash is syntactic sugar for a call to test, which is documented in man test. "or" is expressed by an infix -o, but you need an "and":

while [ $guess != 5 -a $guess != 10 ]; do

There are 2 correct and portable ways to achieve what you want.
Good old shell syntax:

while [ "$guess" != 5 ] && [ "$guess" != 10 ]; do

And bash syntax (as you specify):

while [[ "$guess" != 5 && "$guess" != 10 ]]; do

The portable and robust way is to use a case statement instead. If you are not used to it, it might take a few looks just to wrap your head around the syntax.

while true; do
    case $guess in 5 | 10) break ;; esac
    echo Your answer is $guess. This is incorrect. Please try again.
    echo -n "What is your guess? "
    read guess  # not $guess
done

I used while true but you could in fact use the case statement there directly. It gets hairy to read and maintain, though.

while case $guess in 5 | 10) false;; *) true;; esac; do ...

Tags:

Linux

Bash