Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
なぜこれが機能するのか知りたい:
arr=() fun() { arr[$1]=$2; } fun 1 2 echo ${arr[1]} # echoes '2'
しかし、これはしません:
arr=() fun() { arr[$1]=$2; } fun 1 2 & wait echo ${arr[1]} # echoes a blank line
2 番目の例でバックグラウンドで実行funすることにより、サブシェルで実行します。サブシェルで行われた配列への変更は、 の値をエコーする親シェルには表示されませんarr[1]。
fun
arr[1]
関数を非同期で実行すると、親コンテキストの環境を変更できない新しいシェル コンテキストが作成されるため、これは機能しません。これは、制御構造内で変更された変数がパイプの外側の親では変更されない、制御構造へのパイプとよく似ています。