0

I want to make sure the value of a variable (STATE) is part of a list (ALL_STATES).

#!/usr/bin/bash

STATE='somevalue' ALL_STATES=( lirum larum loeffelstiel )

echo state id is: "${STATE}" echo all states are: "${ALL_STATES[@]}"

while ${STATE} not in ${ALL_STATES} ; do echo ${STATE} needs a valid value read -p "please provide the state ID: " STATE done

echo state id is: "${STATE}"

I am getting ./test_while_loop.sh: line 8: somevalue: command not found though. So there seems to be something wrong with my condition, just what?

ilkkachu
  • 138,973
vrms
  • 139

1 Answers1

0

The error in your script is that in bash, the while keyword must be followed by a command, it does not interpret expressions. Also, <value> in <array> is not a valid expression either.

Instead you could turn the list into an associative array. Consider following the example given in this answer:

https://unix.stackexchange.com/a/177589/573867

jms
  • 89