1

I wrote a shell script to recognize the type of character

printf "Enter a character: "
read var
case "$var" in
[a-z])
        echo "You entered a lower case alphabet."
        ;;
[A-Z])
        echo "You entered an upper case alphabet."
        ;;
[0-9])
        echo "You entered a digit."
        ;;
?)
        echo "You entered a special simbol."
        ;;
*)
        echo "You entered more then one character."
        ;;
esac

But if input letter is whether lower-case or upper-case the output is:

You entered a lower case alphabet.

The output is correct if I write it this way:

printf "Enter a character: "
read var
case "$var" in
[A-Z])
        echo "You entered a upper case alphabet."
        ;;
[a-z])
        echo "You entered an lower case alphabet."
        ;;
[0-9])
        echo "You entered a digit."
        ;;
?)
        echo "You entered a special simbol."
        ;;
*)
        echo "You entered more then one character."
        ;;
esac

Can't figure out why. The only logical answer is [a-z] includes lower-case and upper-case characters.

  • 7
    Try with LANG=C or LC_COLLATE=C. – Mikel Mar 26 '14 at 6:04
  • 5
    Try with [[:lower:]], [[:upper:]] instead. é is not a lower case character is the C locale. – Stéphane Chazelas Mar 26 '14 at 7:44
1

Lexical ordering depends on the locale. Differences are not restricted to "special" characters (e.g. ä like a in German, but after z in Finnish). Instead, in some locales, the ordering might be AaBbCc…, so [a-z] would expand to [aBbCc…Zz]. The patterns [[:lower:]] and [[:upper:]] mentioned by Stephane Chazelas are safe from these surprises.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.