User input validation in Bash

See here for the original answer.

See this thread for an example of validating user input in Bash.

Here is the code modified for your use case:

#!/usr/bin/bash

REGEX='^[YyNn]$'

CHECK="your_user_input_var"

if [[ ! $CHECK =~ $REGEX ]]
then
  echo "Not ok"
else
  echo "ok"
fi

Tags

  1. bash (Private)
  2. regex (Private)
  3. input (Private)
  4. validation (Private)
  5. stack-overflow (Private)
  6. answer (Private)