Case sensitivity in shell scripting
The problem is that the character range [a-z]
actually includes the upper case letters. This is explained in the bash manual:
Within a bracket expression, a range expression consists of two characters separated by a hyphen. It matches any single character that sorts between the two characters, inclusive. In the default C locale, the sorting sequence is the native character order; for example, ‘[a-d]’ is equivalent to ‘[abcd]’. In other locales, the sorting sequence is not specified, and ‘[a-d]’ might be equivalent to ‘[abcd]’ or to ‘[aBbCcDd]’, or it might fail to match any character, or the set of characters that it matches might even be erratic. To obtain the traditional interpretation of bracket expressions, you can use the ‘C’ locale by setting the LC_ALL environment variable to the value ‘C’.
To illustrate:
$ case B in [a-c]) echo YES;; *) echo NO;; esac
YES
$ LC_ALL=C; case B in [a-c]) echo YES;; *) echo NO;; esac
NO
So, what happens is that in your locale (which is not C
), [a-c]
is actually [aAbBcC]
. That's why you should use the POSIX character classes as suggested by @karel instead.
#!/bin/bash
echo 'enter any character'
read char
case $char in
[[:lower:]]) echo 'lower case letter'
;;
[[:upper:]]) echo 'upper case letter'
;;
[0-9]) echo 'number'
;;
?) echo 'special char'
;;
*) echo 'u entered more than one char'
;;
esac
For more information about the lower case regular expression of [a-z] and the upper case regular expression of [A-Z] in bash see Why isn't the case statement case-sensitive when nocasematch is off?.