Ubuntu – Case sensitivity in shell scripting

bashscripts

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?

Best Answer

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