You can do:
[[ $var = +([[:alnum:]]) ]]
That would work in the AT&T ksh and zsh
implementations of ksh
, but apparently not in pdksh
-based ones. That works in zsh -o kshglob
(like when zsh
is invoked as ksh
) or bash -O extglob
as well.
+(...)
is ksh
wildcard for one or more. [[:alnum:]]
is any character considered alphanumeric in the current locale (in any alphabet, not necessarily only the latin alphabet).
If you want to limit to the English letters and digits, assuming the LC_ALL
variable is not set, you could do:
LC_COLLATE=C; [[ $var = +([a-zA-Z0-9]) ]]
If not:
[[ $var = +([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]) ]]
This:
LC_ALL=C; [[ $var = +([[:alnum:]]) ]]
Should also work even though it changes the meaning of characters. Because characters that would contain bytes that otherwise correspond to ASCII alnums (like for instance £ in GB18030 which is encoded as 81 30 84 35 where 30 also happens to be ASCII 0) would also contain bytes that are not in ASCII (like 81 84 for £), and all charsets on a given system have to agree on the encoding of the characters in the portable charset which includes all the English alphanumerics.
Also note that in UTF-8 locales, ksh93u+ (at least) currently has a bug in that if $var
contains sequences of bytes that don't form valid characters, but those bytes correspond to alnums in the ISO-8859-1 character set, then they would be considered as alnums
. For instance $'A\xe9B'
would be considered as an alphanumeric because 0xe9 is é in ISO-8859-1. (U+00E9 is é
, but the UTF-8 encoding of é is 0xc3 0xa9, not 0xe9).