2

Ok, so I'm trying to run a script where if the user enters y or Y, then the output is YES but if the user inputs n or N then the output is NO, this is what I have so far:

read character
if (( ("$character") == "y" )) || (( ("$character" == "Y") )); then
echo "YES"
else
echo "NO"
fi

When I run this code I get YES successfully but inputting n just results in YES anyway.

I have no idea what I'm doing wrong and any I'd love some input.

Thanks in advance!

1 Answer 1

4

((...)) is used for math calculations. You can use [[...]] in BASH like this:

read character
if [[ "$character" == [yY] ]]; then
   echo "YES"
else
   echo "NO"
fi

Also I have combined 2 conditions into one using [yY] glob pattern.

Sign up to request clarification or add additional context in comments.

1 Comment

[yY] is also a glob pattern that is matched by ==

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.