logo

バッシュ配列

このトピックでは、bash 配列の基本と、それらが bash シェル スクリプトでどのように使用されるかを説明します。

配列は、同様のタイプの要素のコレクションとして定義できます。ほとんどのプログラミング言語とは異なり、bash スクリプトの配列は同様の要素のコレクションである必要はありません。 Bash は文字列と数値を区別しないため、配列には文字列と数値の両方が含まれる場合があります。

Bash は多次元配列をサポートしていません。それ自体が配列である要素を持つことはできません。 Bash は、数値インデックス付きの 1 次元配列と連想配列をサポートします。数値インデックス付き配列に最後からアクセスするには、負のインデックスを使用できます。 「-1」のインデックスは最後の要素の参照とみなされます。配列内で複数の要素を使用できます。

Bash 配列宣言

Bash の配列は次の方法で宣言できます。

数値インデックス付き配列の作成

宣言せずに、任意の変数をインデックス付き配列として使用できます。

変数を Bash 配列として明示的に宣言するには、キーワード 'declare' を使用します。構文は次のように定義できます。

 declare -a ARRAY_NAME 

どこ、

ARRAY_NAME は、配列に割り当てる名前を示します。

注記:Bash で変数に名前を付ける規則は、配列に名前を付ける場合と同じです。

インデックス付き配列を作成する一般的なメソッドは、次の形式で定義できます。

 ARRAY_NAME[index_1]=value_1 ARRAY_NAME[index_2]=value_2 ARRAY_NAME[index_n]=value_n 

ここで、キーワード「index」は正の整数を定義するために使用されます。

連想配列の作成

数値インデックス付き配列とは異なり、連想配列は最初に宣言されます。キーワード「declare」と -A (大文字) オプションを使用して、連想配列を宣言できます。構文は次のように定義できます。

 declare -A ARRAY_NAME 

連想配列を作成する一般的なメソッドは、次の形式で定義できます。

 declare -A ARRAY_NAME ARRAY_NAME[index_foo]=value_foo ARRAY_NAME[index_bar]=value_bar ARRAY_NAME[index_xyz]=value_xyz 

ここで、index_ は文字列を定義するために使用されます。

上記のフォームは次のように記述することもできます。

Java文字列をブール値に変換
 declare -A ARRAY_NAME ARRAY_NAME=( [index_foo]=value_foo [index_bar]=value_bar [index_xyz]=value_xyz ) 

Bash 配列の初期化

Bash 配列を初期化するには、次のように括弧内の要素のリストをスペースで区切って指定することで、代入演算子 (=) を使用できます。

 ARRAY_NAME=(element_1st element_2nd element_Nth) 

注記:ここで、最初の要素のインデックスは 0 になります。また、代入演算子 (=) の周囲にスペースを入れてはいけません。

Bash 配列の要素にアクセスする

Bash 配列の要素にアクセスするには、次の構文を使用できます。

 echo ${ARRAY_NAME[2]} 

Bash 配列を印刷する

キーワード「declare」を「-p」オプションとともに使用すると、Bash 配列のすべての要素をすべてのインデックスと詳細とともに出力できます。 Bash 配列を出力する構文は次のように定義できます。

 declare -p ARRAY_NAME 

配列演算

配列が割り当てられると、それに対していくつかの便利な操作を実行できるようになります。キーと値を表示したり、要素を追加または削除して変更したりできます。

参照要素

単一の要素を参照するには、要素のインデックス番号を知る必要があります。次の構文を使用して、任意の要素を参照または出力できます。

 ${ARRAY_NAME[index]} 

注記:シェルのファイル名展開演算子を回避するには、中括弧 ${} が必要です。

たとえば、インデックス 2 の配列の要素を出力してみましょう。

Bash スクリプト

 #!/bin/bash #Script to print an element of an array with an index of 2 #declaring the array declare -a example_array=( 'Welcome''To''Javatpoint' ) #printing the element with index of 2 echo ${example_array[2]} 

出力

 Javatpoint 

指定したインデックスの代わりに @ または * を使用すると、配列のすべてのメンバーに展開されます。すべての要素を出力するには、次の形式を使用できます。

Bash スクリプト

 #!/bin/bash #Script to print all the elements of the array #declaring the array declare -a example_array=( 'Welcome''To''Javatpoint' ) #Printing all the elements echo '${example_array[@]}' 

出力

 Welcome to Javatpoint 

@ と * の使用の唯一の違いは、@ を使用する場合、フォームが二重引用符で囲まれることです。最初のケース (@ を使用している場合) では、展開により配列の各要素の結果がワードで提供されます。これは、「for ループ」を使用するとよりよく説明できます。 「Welcome」、「To」、「Javatpoint」という 3 つの要素を含む配列があると仮定します。

 $ example_array= (Welcome to Javatpoint) 

@ を使用してループを適用する:

 for i in '${example_array[@]}'; do echo '$i'; done 

次の結果が生成されます。

 Welcome To Javatpoint 

* を使用してループを適用すると、配列のすべての要素を 1 つの単語として保持する 1 つの結果が生成されます。

 Welcome To Javatpoint 

@ と * の使用法を理解することは重要です。これは、フォームを使用して配列要素を反復処理するときに便利です。

bash for ループ

配列のキーの出力

それぞれの値の代わりに、インデックス付き配列または連想配列で使用されているキーを取得して出力することもできます。 !を追加することで実行できます。以下のように配列名の前に演算子を追加します。

COBOLプログラミング
 ${!ARRAY_NAME[index]} 

 #!/bin/bash #Script to print the keys of the array #Declaring the Array declare -a example_array=( 'Welcome''To''Javatpoint' ) #Printing the Keys echo '${!example_array[@]}' 

出力

 0 1 2 

配列の長さを求める

次の形式を使用して、配列に含まれる要素の数をカウントできます。

 ${#ARRAY_NAME[@]} 

 #!/bin/bash #Declaring the Array declare -a example_array=( 'Welcome''To''Javatpoint' ) #Printing Array Length echo 'The array contains ${#example_array[@]} elements' 

出力

 The array contains 3 elements 

配列をループする

配列内の各項目を反復する一般的な方法は、「for ループ」を使用することです。

 #!/bin/bash #Script to print all keys and values using loop through the array declare -a example_array=( 'Welcome''To''Javatpoint' ) #Array Loop for i in '${!example_array[@]}' do echo The key value of element '${example_array[$i]}' is '$i' done 

出力

バッシュ配列

配列をループするもう 1 つの一般的な方法は、配列の長さを取得して C スタイルのループを使用することです。

Bash スクリプト

 #!/bin/bash #Script to loop through an array in C-style declare -a example_array=( &apos;Welcome&apos;&apos;To&apos;&apos;Javatpoint&apos; ) #Length of the Array length=${#example_array[@]} #Array Loop for (( i=0; i <${length}; i++ )) do echo $i ${example_array[$i]} done < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/bash-tutorial/77/bash-array-2.webp" alt="Bash Array"> <h3>Adding Elements to an Array</h3> <p>We have an option to add elements to an indexed or associative array by specifying their index or associative key respectively. To add the new element to an array in bash, we can use the following form:</p> <pre> ARRAY_NAME[index_n]=&apos;New Element&apos; </pre> <p> <strong>Example</strong> </p> <pre> #!/bin/bash #Declaring an array declare -a example_array=( &apos;Java&apos;&apos;Python&apos;&apos;PHP&apos;&apos;HTML&apos; ) #Adding new element example_array[4]=&apos;JavaScript&apos; #Printing all the elements echo &apos;${example_array[@]}&apos; </pre> <p> <strong>Output</strong> </p> <pre> Java Python PHP HTML JavaScript </pre> <p>Another method for adding a new element to an array is by using the += operator. There is no need to specify the index in this method. We can add one or multiple elements in the array by using the following way:</p> <p> <strong>Example</strong> </p> <pre> #!/bin/bash #Declaring the Array declare -a example_array=( &apos;Java&apos;&apos;Python&apos;&apos;PHP&apos; ) #Adding new elements example_array+=( JavaScript CSS SQL ) #Printing all the elements echo &apos;${example_array[@]}&apos; </pre> <p> <strong>Output</strong> </p> <pre> Java Python PHP JavaScript CSS SQL </pre> <h3>Updating Array Element</h3> <p>We can update the array element by assigning a new value to the existing array by its index value. Let&apos;s change the array element at index 4 with an element &apos;Javatpoint&apos;.</p> <p> <strong>Example</strong> </p> <pre> #!/bin/bash #Script to update array element #Declaring the array declare -a example_array=( &apos;We&apos;&apos;welcome&apos;&apos;you&apos;&apos;on&apos;&apos;SSSIT&apos; ) #Updating the Array Element example_array[4]=Javatpoint #Printig all the elements of the Array echo ${example_array[@]} </pre> <p> <strong>Output</strong> </p> <pre> We welcome you on Javatpoint </pre> <h3>Deleting an Element from an Array</h3> <p>If we want to delete the element from the array, we have to know its index or key in case of an associative array. An element can be removed by using the &apos; <strong>unset</strong> &apos; command:</p> <pre> unset ARRAY_NAME[index] </pre> <p>An example is shown below to provide you a better understanding of this concept:</p> <p> <strong>Example</strong> </p> <pre> #!/bin/bash #Script to delete the element from the array #Declaring the array declare -a example_array=( &apos;Java&apos;&apos;Python&apos;&apos;HTML&apos;&apos;CSS&apos;&apos;JavaScript&apos; ) #Removing the element unset example_array[1] #Printing all the elements after deletion echo &apos;${example_array[@]}&apos; </pre> <p> <strong>Output</strong> </p> <pre> Java HTML CSS JavaScript </pre> <p>Here, we have created a simple array consisting of five elements, &apos;Java&apos;, &apos;Python&apos;, &apos;HTML&apos;, &apos;CSS&apos; and &apos;JavaScript&apos;. Then we removed the element &apos;Python&apos; from the array by using &apos;unset&apos; and referencing the index of it. The index of element &apos;Python&apos; was &apos;1&apos;, since bash arrays start from 0. If we check the indexes of the array after removing the element, we can see that the index for the removed element is missing. We can check the indexes by adding the following command into the script:</p> <pre> echo ${!example_array[@]} </pre> <p>The output will look like:</p> <pre> 0 2 3 4 </pre> <p>This concept also works for the associative arrays.</p> <h3>Deleting the Entire Array</h3> <p>Deleting an entire array is a very simple task. It can be performed by passing the array name as an argument to the &apos; <strong>unset</strong> &apos; command without specifying the index or key.</p> <p> <strong>Example</strong> </p> <pre> #!/bin/bash #Script to delete the entire Array #Declaring the Array declare -a example_array=( &apos;Java&apos;&apos;Python&apos;&apos;HTML&apos;&apos;CSS&apos;&apos;JavaScript&apos; ) #Deleting Entire Array unset example_array #Printing the Array Elements echo ${!example_array[@]} #Printing the keys echo ${!example_array[@]} </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/bash-tutorial/77/bash-array-3.webp" alt="Bash Array"> <p>There will be no output if we try to print the content of the above script. An empty result is returned because the array doesn&apos;t exist anymore.</p> <h3>Slice Array Elements</h3> <p>Bash arrays can also be sliced from a given starting index to the ending index.</p> <p>To slice an array from starting index &apos;m&apos; to an ending index &apos;n&apos;, we can use the following syntax:</p> <pre> SLICED_ARRAY=(${ARRAY_NAME[@]:m:n}&apos;) </pre> <p> <strong>Example</strong> </p> <pre> #!/bin/bash #Script to slice Array Element from index 1 to index 3 #Declaring the Array example_array=( &apos;Java&apos;&apos;Python&apos;&apos;HTML&apos;&apos;CSS&apos;&apos;JavaScript&apos; ) #Slicing the Array sliced_array=(&apos;${example_array[@]:1:3}&apos;) #Applying for loop to iterate over each element in Array for i in &apos;${sliced_array[@]}&apos; do echo $i done </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/bash-tutorial/77/bash-array-4.webp" alt="Bash Array"> <hr></${length};>

 #!/bin/bash #Declaring an array declare -a example_array=( &apos;Java&apos;&apos;Python&apos;&apos;PHP&apos;&apos;HTML&apos; ) #Adding new element example_array[4]=&apos;JavaScript&apos; #Printing all the elements echo &apos;${example_array[@]}&apos; 

出力

 Java Python PHP HTML JavaScript 

新しい要素を配列に追加するもう 1 つの方法は、+= 演算子を使用することです。このメソッドではインデックスを指定する必要はありません。次の方法を使用して、配列に 1 つまたは複数の要素を追加できます。

 #!/bin/bash #Declaring the Array declare -a example_array=( &apos;Java&apos;&apos;Python&apos;&apos;PHP&apos; ) #Adding new elements example_array+=( JavaScript CSS SQL ) #Printing all the elements echo &apos;${example_array[@]}&apos; 

出力

 Java Python PHP JavaScript CSS SQL 

配列要素の更新

インデックス値によって既存の配列に新しい値を割り当てることで、配列要素を更新できます。インデックス 4 の配列要素を要素「Javatpoint」に変更してみましょう。

 #!/bin/bash #Script to update array element #Declaring the array declare -a example_array=( &apos;We&apos;&apos;welcome&apos;&apos;you&apos;&apos;on&apos;&apos;SSSIT&apos; ) #Updating the Array Element example_array[4]=Javatpoint #Printig all the elements of the Array echo ${example_array[@]} 

出力

 We welcome you on Javatpoint 

配列からの要素の削除

配列から要素を削除したい場合は、そのインデックスまたは連想配列の場合はキーを知る必要があります。要素は「」を使用して削除できます。 設定を解除する ' 指示:

 unset ARRAY_NAME[index] 

この概念をより深く理解できるように、以下に例を示します。

 #!/bin/bash #Script to delete the element from the array #Declaring the array declare -a example_array=( &apos;Java&apos;&apos;Python&apos;&apos;HTML&apos;&apos;CSS&apos;&apos;JavaScript&apos; ) #Removing the element unset example_array[1] #Printing all the elements after deletion echo &apos;${example_array[@]}&apos; 

出力

 Java HTML CSS JavaScript 

ここでは、「Java」、「Python」、「HTML」、「CSS」、「JavaScript」の 5 つの要素で構成される単純な配列を作成しました。次に、「unset」を使用し、そのインデックスを参照することにより、要素「Python」を配列から削除しました。 bash 配列は 0 から始まるため、要素 'Python' のインデックスは '1' でした。要素を削除した後に配列のインデックスを確認すると、削除された要素のインデックスが欠落していることがわかります。次のコマンドをスクリプトに追加することで、インデックスを確認できます。

 echo ${!example_array[@]} 

出力は次のようになります。

 0 2 3 4 

この概念は連想配列にも当てはまります。

アレイ全体の削除

アレイ全体の削除は非常に簡単な作業です。これは、配列名を引数として ' に渡すことで実行できます。 設定を解除する ' インデックスまたはキーを指定せずにコマンドを実行します。

 #!/bin/bash #Script to delete the entire Array #Declaring the Array declare -a example_array=( &apos;Java&apos;&apos;Python&apos;&apos;HTML&apos;&apos;CSS&apos;&apos;JavaScript&apos; ) #Deleting Entire Array unset example_array #Printing the Array Elements echo ${!example_array[@]} #Printing the keys echo ${!example_array[@]} 

出力

.tostring java
バッシュ配列

上記のスクリプトの内容を出力しようとしても、出力はありません。配列はもう存在しないため、空の結果が返されます。

配列要素をスライスする

Bash 配列は、指定された開始インデックスから終了インデックスまでスライスすることもできます。

開始インデックス「m」から終了インデックス「n」まで配列をスライスするには、次の構文を使用できます。

 SLICED_ARRAY=(${ARRAY_NAME[@]:m:n}&apos;) 

 #!/bin/bash #Script to slice Array Element from index 1 to index 3 #Declaring the Array example_array=( &apos;Java&apos;&apos;Python&apos;&apos;HTML&apos;&apos;CSS&apos;&apos;JavaScript&apos; ) #Slicing the Array sliced_array=(&apos;${example_array[@]:1:3}&apos;) #Applying for loop to iterate over each element in Array for i in &apos;${sliced_array[@]}&apos; do echo $i done 

出力

バッシュ配列