I am working on a little script that plays a game. At the end, it asks "Play again? [Y/N]" and I would like the game to loop when the user inputs any word starting with 'y'.
My code:
playAgain="yes" while [[ "$playAgain" =~ ^y* ]]; do # working game loop code read -p "Play Again? (y/n): " playAgain done echo "Thank you for playing!" at this time the game loop will execute regardless of what is in "playAgain".
Even it I initialize playAgain to "no" the game loop will still execute.
No matter what I type into the "read" call for playAgain, the game loop will execute.
I got the regex syntax from here.
Y?foo == y*is doing a globbing comparison whilefoo =~ ^y.*is doing a regexp comparison. It looks like you took the=~from an answer doing a regexp comparison but they*from an answer doing a globbing comparison. Globbing and regexps are both used for pattern matching and they look similar but their syntax/metachars (e.g.*) have different meanings.