zKaj
zKaj

Reputation: 9

Bash upper/lowercase check to case statement

I have following upper or lower case character check written using if conditional, how to do this with case statement?

char=""
echo "Type char:"
read char

if [ $char = ${char^} ]; then
echo "upper case"
elif [ $char = ${char,,} ]; then
echo "lower case"
else
echo "neither"
fi

Upvotes: 0

Views: 2022

Answers (1)

pynexj
pynexj

Reputation: 20688

For example:

[STEP 101] $ cat foo.sh
shopt -s extglob
read -p "Type char: " ch
case $ch in
    +([[:lower:]])) echo lowercase;;
    +([[:upper:]])) echo uppercase;;
    *)              echo neither;;
esac
[STEP 102] $ ./foo.sh
Type char: abc
lowercase
[STEP 103] $ ./foo.sh
Type char: ABC
uppercase
[STEP 104] $ ./foo.sh
Type char: aBc
neither
[STEP 105] $

See shopt -s extglob and pattern matching in Bash's manual.

Upvotes: 2

Related Questions