A way to use test for "$x" = [a-z] (regex)

In bourne, I was hoping to do:

if test "$x" = [a-z]

but even when x equals "q" (not with quotes) test returns 1.

Is there a way to do this?

For regular expressions I normally use case....

case "$x" in
[a-z] )
        echo lowercase
        ;;
* )
        echo not lowercase
        ;;
esac

Thanks. I'll try that

Hmm, how do I create a regular expression for NOT [a-z]? In Bourne, [^a-z] gave me an error message:

expr: unmatched [ or [^

The ultimate goal is to test that $x is one character, no more no less, and is [a-z]

Really easy:

case "$x" in
  [a-z]) echo "I don't want this because it's lowercase";;
  * ) echo "Yippee! It's not lowercase";;
esac

:slight_smile:

You're using globbing, not regular expressions :slight_smile:

Isn't globbing to do with filenames?

The Bourne shell used the same pattern concept for both filename expansion and for matching in case statements. These patterns are different than the regular expressions used in sed and the other editors. Still globbing is wrong word to use in reference to case statements. Most shells have an option to turn off globbing. If you do that, patterns still work in case statements. "globbing" really means just filename expansion.

I stand corrected: the correct term shuold be pattern matching.
As Perderabo said, the Unix library function glob and the case statement use the same pattern matching notation, and, as prowla and Perderabo mentioned, the word "globbing" refers only to file name expansion.

So you can use just:

case $x in [a-z]) "it's OK, put your code here" ;;esac

Anyway,
as far as the message "expr: unmatched [ or [^",
is concerned, you should use [!a-z] instead of [^a-z].