
Next Bash(v 5.1.4)スクリプト
#!/usr/bin/env bash
echo $BASHOPTS
shopt extglob
x="123"
if [[ "$x" == +([[:digit:]]) ]]; then
echo "[[ Ok!"
fi
case $x in
+([[:digit:]])) echo "case Ok" ;;
*) echo case "KO" ;;
esac
出力
checkwinsize:cmdhist:complete_fullquote:extquote:force_fignore:globasciiranges:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
extglob off
[[ Ok!
./extglob_test.sh: line 10: syntax error near unexpected token `('
./extglob_test.sh: line 10: ` +([[:digit:]])) echo "case Ok" ;;'
これは私にいくつかの質問をしました。
shopt extglob
閉じていると言うと、9行のケースオプションが論理的に失敗します。しかし、コードの5行目がなぜ成功するのですか?環境変数BASHOPTSには継承されたshoptオプションが含まれており、BASHのマニュアルには次のように指定されています。
BASHOPTS A colon-separated list of enabled shell options. Each word in the list is a valid argument for the -s option to the shopt builtin command (see SHELL BUILTIN COMMANDS below). The options appearing in BASHOPTS are those reported as on by shopt. If this variable is in the environment when bash starts up, each shell option in the list will be enabled before reading any startup files. This variable is read-only.
extglob
だからスクリプトで有効になっているのを見たいです。実際にecho $BASHOPTS
シェルプロンプトでコマンドを実行すると、次のように出力されます。checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:histappend:interactive_comments:progcomp:promptvars:sourcepath
extglob
ただし、ここにはありますが、スクリプト出力には欠けているオプションなど、いくつかの違いがあります。もちろん、スクリプトの3行目でオプションを有効にすると、次の結果が
extglob
表示されます。shopt -s extglob
checkwinsize:cmdhist:complete_fullquote:extquote:force_fignore:globasciiranges:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath [[ Ok! case Ok
しかし、リンター(私はVimでAleを使用しています)はまだ9行目で構文エラー信号を送信します。
どんなアイデアがありますか?
答え1
質問が3つあります。
コメントからわかるように、
[[
内部extglobはデフォルトで有効になっています。環境変数BASHOPTSはありません。走ると何も見えませんが、
export | grep BASHOPTS
何も見えません。あなたが引用したように」もしこの変数は bash の起動時に環境にあります..."export BASHOPTS
対話型シェルで実行する必要があります。より良いリントを手に入れよう!