Celeb Glow
general | April 03, 2026

Case sensitivity in shell scripting

Consider this Bash script:

#!/bin/bash
echo Enter any character
read char
case $char in [a-z]) echo Lower case letter ;; [A-Z]) echo Upper case letter ;; [0-9]) echo Number ;; ?) echo Special char ;; *) echo You entered more than one character ;;
esac

If I enter 'a', the output is Lower case letter, and it is the same for 'A'... How do I overcome this?

3

2 Answers

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.

1
#!/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?.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy