如果我在命令行中输入:
_longopt ()
{
case "$prev" in
--+([-a-z0-9_]))
echo foo
;;
esac;
}
它按预期工作。
如果我将它放入脚本(foo.sh)中,它会失败:
./foo.sh: line 6: syntax error near unexpected token `('
./foo.sh: line 6: ` --+([-a-z0-9_]))'
为什么?
更重要的是:鉴于我已经定义了一个像上面这样的函数,我如何将它自动转换为在脚本中工作的函数?
$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
答案1
你需要:
shopt -s extglob
感谢@Stéphane Chazales 向我指出 extglob。