0

I know in Python there's a way to check if there is a certain item in an array or a character in a string and I'm looking for something like this bash.

My script takes a user input (y/n) and uses a while loop to check if input matches either "y" or "n" and this works however I want the script to be non case sensitive meaning I need 4 different checks (YyNa) and I'm sure there's a simpler way to do it.

Something like:

while $var not in "YyNn"

1
  • 1
    What has your research shown? Commented Sep 24, 2022 at 12:08

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.