Bashスクリプトで拡張されたglobパターンの一貫性のない動作

Bashスクリプトで拡張されたglobパターンの一貫性のない動作

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" ;;'

これは私にいくつかの質問をしました。

  1. shopt extglob閉じていると言うと、9行のケースオプションが論理的に失敗します。しかし、コードの5行目がなぜ成功するのですか?

  2. 環境変数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
    
  3. しかし、リンター(私はVimでAleを使用しています)はまだ9行目で構文エラー信号を送信します。

どんなアイデアがありますか?

ベストアンサー1

質問が3つあります。

  1. コメントからわかるように、[[内部extglobはデフォルトで有効になっています。

  2. 環境変数BASHOPTSはありません。走ると何も見えませんが、export | grep BASHOPTS何も見えません。あなたが引用したように」もしこの変数は bash の起動時に環境にあります..."export BASHOPTS対話型シェルで実行する必要があります。

  3. より良いリントを手に入れよう!

おすすめ記事