Question:

Using an if statement for a list of words

Lucas: 02 February 2022

So I have a program (say, programA), that will give me an output, for example: yes, no, maybe, probably, possibly, impossible, banana.

I want to make a script that will do something, whatever it is, based on that output. Let's say I only need to account for yes, maybe and banana.

So far what I would do, is use case like so:

case $program_output in
  yes) echo "good word: $program_output" ;;
  maybe) echo "good word: $program_output" ;;
  banana) echo "good word: $program_output" ;;
  *) echo "bad word: $program_output" ;;
esac

But recently I was fiddling with the if statement, and found out I can do this faster like so:

if [[ "yesmaybebanana" =~ ${program_output} ]]; then
  echo "good word: ${program_output}"; else echo "bad word: ${program_output}";
fi

Is there any reason why I should not use the if statement for this? This is a case where $program_output cannot have spaces in it, and it's a limited list of words it can output.

Answer:
Ryder: 02 February 2022

If you want to use bash's regular expression functions (which however are bash-specific and therefore not portable), you should at least use the proper "OR"-type operator to enforce exact match with one of the allowed words, as in

if [[ $program_output =~ ^(yes|maybe|banana)$ ]]
then
    echo "Good word: $program_output"
else
    echo "Bad word: $program_output"
fi

Notice that there must be no quotes around the regular expression, and that the word alternatives are enclosed between ^ and $ anchors to ensure that no sub-string can trigger a match (such as yessir which would otherwise be caught by the pattern yes).