bash case 语句中的复合模式

mbo*_*nin 2 bash

我无法获得http://www.gnu.org/software/bash/manual/bashref.html#Pattern-Matching中所述的复合模式来匹配我的 case 语句:

i=33; case $i in *([A-Za-z0-9]) ) echo "alphanumeric";; * ) echo "bah";; esac;
Run Code Online (Sandbox Code Playgroud)

我希望上面的脚本显示“字母数字”,但它失败了:

bash: syntax error near unexpected token `('
Run Code Online (Sandbox Code Playgroud)

知道出了什么问题吗?

B J*_*son 5

extglob尝试像这样打开shell 选项:

shopt -s extglob
Run Code Online (Sandbox Code Playgroud)

然后您可以使用扩展模式匹配运算符:

i='33'; case $i in *([A-Za-z0-9]) ) echo "alphanumeric";; * ) echo "bah";; esac;
Run Code Online (Sandbox Code Playgroud)

从手册页:

extglob - 如果设置,则启用上面在路径名扩展下描述的扩展模式匹配功能。