I am trying to perform a simple while loop comparing a variable to a string. It fails to load with the error on this line. Error states [: missing `]’ and : command not found. My while loop looks like;
mainMenuInput = "" while ["$mainMenuInput" != "Q" || "$mainMenuInput" != "q"] do
Advertisement
Answer
There are a couple of errors:
mainMenuInput="" while [ "$mainMenuInput" != "Q" ] && [ "$mainMenuInput" != "q" ]
See that variable declaration do need to be like var=value
. Otherwise, bash would interpret that you want to perform the var
command with =
and value
as parameters:
mainMenuInput="" ^ no spaces around =
In the while
you need to put spaces around the brackets. Also, note that you need to use &&
(and) instead of ||
(or), because otherwise it won’t ever exit the while
.
while [ "$mainMenuInput" != "Q" ] && [ "$mainMenuInput" != "q" ] ^ ^^ ^ space it has to be AND space
In fact, the condition can be rewritten to something like:
while [[ "$mainMenuInput" != [qQ] ]]