2014-10-28

How to watch for correct input in bash

I've seen some pretty crazy ways to do this. Here's how I do it, which I think is the simplest way.
#!/bin/bash

while true; do
    read -p 'Prompt? (y/n) ' yn
    case "$yn" in
        y|Y)
            echo 'YES!'
            break;
            ;;
        n|N)
            echo 'NO!'
            break;
            ;;
    esac
done
This will loop until one of [yYnN] is entered.