
コマンドラインに次のように入力した場合:
_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
私にextglobを指摘してくれた@Stéphane Chazalesに感謝します。