digits or symbols are characters. It looks like you want either:
- only alphabetical characters (
[[:alpha:]]
)
- or possibly alphabetical characters but only in the latin script (as your
a-z
suggests)
- or possibly alphabetical character in the latin script and without diacritics.
Unless the locale is C/POSIX what [a-z]
matches is more or less random in bash
(on GNU systems at least).
For 1, you'd want:
die() {
printf >&2 '%s\n' "$1"
exit 1
}
case $string in
("") die "Can't be empty";;
(*[![:alpha:]]*) die "contains non-alphabetical characters";;
(*) echo OK
esac
That would accept all
, Stéphane
(Latin script), γράμμα
(Greek script), письмо
(Cyrillic), but not foo-bar
, 123
...
2 can be tricky, especially if you want to consider combining diacritics.
For 3, for it to run in any locale, you'd need to specify the characters you want:
ok=abcdefghijklmnopqstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
and in the case
statement, use
(*[!$ok]*) die "contains characters not allowed";;
Or you could switch to zsh
where ranges like [a-zA-Z]
are based on character code points, so always only include abcdefghijklmnopqstuvwxyz or with bash 4.3 or newer use theglobasciiranges
option to have the same behaviour in bash
.