BASH - case statement

Hi Gurus,

I have the below BASH code which does not works for upper case alphabets except Z (upper case Z).

What may be the reason. Also escape sequences like \n, \t, \b, \033(1m \033(0m (For bold letter) are not working.[It works few days before]

case $var in
[a-z])
          echo "Lower case alphabet"
          ;;
[A-Z]
          echo "upper case alphabet"
          ;;
esac

Thanks,
Ganesh

The first argument to a bash shell script is usually assigned to $1. Are you assigning that to your $var variable elsewhere in your script? If you change $var to $1 it works fine.

Missing something?

case $var in
[a-z])
          echo "Lower case alphabet"
          ;;
[A-Z])
          echo "upper case alphabet"
          ;;
esac

Chaged $var as $1 - It's not working

Below is the full code - I cant able to find out what causes this error

echo "Enter the character"
read var
case $var in
[a-z])
         echo "Entered a lowercase alphabet"
         ;;
[A-Z])
         echo "Entered a uppercase alphabet"
         ;;
[0-9])
         echo "Entered a numeric value"
         ;;
?)
         echo "Entered a special character"
         ;;
*)
         echo "Entered more than one character"
         ;;
esac

Put quotes around the $var variable in the case statement:

echo "Enter the character: "
read var
case "$var" in
[a-z])
    echo "Entered a lowercase alphabet"
    ;;
[A-Z])
    echo "Entered a uppercase alphabet"
    ;;
[0-9])
    echo "Entered a numeric value"
    ;;
?)
    echo "Entered a special character"
    ;;
*)
    echo "Entered more than one character"
    ;;
esac

except uppercase all four other cases are working.

But uppercase is working only for Z(upper case Z)
Bash throws as "Entered lower case alphabet" for all other letters[A-Y]

Unless the shell's case-statement implementation is buggy, double-quoting won't make a difference, since it only prevents expansions which won't occur anyway (field splitting and pathname expansion).

Regards,
Alister

---------- Post updated at 10:06 AM ---------- Previous update was at 10:00 AM ----------

That sounds like a locale collation issue to me. Your locale probably interleaves upper and lower case letters. Either switch to the C (aka POSIX) locale or use character classes, [[:upper:]] and [[:lower:]], which are valid in all locales.

Regards,
Alister

Try:-

["a-z"])

and

["A-Z"])

EDIT:
My error only works for a or z or A or Z...

---------- Post updated at 03:32 PM ---------- Previous update was at 03:10 PM ----------

It works longhand:-

[abcdefghijklmnopqrstuvwxyz])

and

[ABCDEFGHIJKLMNOPQRSTUVWXYZ])

From a cygwin session:-

AMIGA:~> while true; do read var; case "$var" in [abcdefghijklmnopqrstuvwxyz]) echo "Lower case alphabet $var" ;; [ABCDEFGHIJKLMNOPQRSTUVWXYZ]) echo "Upper case alphabet $var" ;; esac; done
a
Lower case alphabet a
A
Upper case alphabet A
f
Lower case alphabet f
F
Upper case alphabet F
K
Upper case alphabet K
k
Lower case alphabet k
Z
Upper case alphabet Z
z
Lower case alphabet z

AMIGA:~> _