检查字符类型

基本类型 / 字符类型

c1="A"
[[ "$c1" == [[:alpha:]] ]] && isLetter=true || isLetter=false
[[ "$c1" == [[:digit:]] ]] && isNumber=true || isNumber=false
#isLetter is true
#isNumber is false

c2='$'
[[ "$c2" == [[:punct:]] ]] && isPunct=true || isPunct=false
#isPunct is true

#加引号后,变量在模式中是纯文本;
#空的 c2 会匹配任何列表,所以要用 -n 检查
symbolList='@#$%^&*'
[[ -n "$c2" && "$symbolList" == *"$c2"* ]] && isInList=true || isInList=false
#isInList is true

echo "isLetter is $isLetter"
echo "isNumber is $isNumber"
echo "isPunct is $isPunct"
echo "isInList is $isInList"