Reputation: 886
I want to receive a string (one word) from the user, with the following criteria: The string may contain only alphabetical characters (aA-zZ) and underscores. Digits and other characters are not allowed.
How may I do this in BASH?
Upvotes: 1
Views: 18406
Reputation: 1713
Use =~
to check a string against a (POSIX extended) regex. See manpages bash(1)
and regex(7)
for more.
# assume your string is in variable $s
if [[ $s =~ ^[A-Za-z_]+$ ]]; then
# it matches
else
# doesn't match
fi
Upvotes: 12