You can track whether to quote a variable named invalid_choice
invalid_choice=true while $invalid_choice; do read choice if [ "$choice" = "yes" ]; then invalid_choice=false ... elif [ "$choice" = "no" ]; then invalid_choice=false ... else echo "Please say yes or no" done
Or you could generalize it to a function if you need to do this a lot:
function confirm() { local ACTION="$1" read -p "$ACTION (y/n)? " -n 1 -r -t 10 REPLY echo "" case "$REPLY" in y|Y ) return 0 ;; * ) return 1 ;; esac } confirm "Do something dangerous" || exit
source share