/usr/bin/bash: Difference between revisions

Line 592: Line 592:


=== Arrays ===
=== Arrays ===
      Bash  provides one-dimensional indexed and associative array variables.  Any variable may be used
Bashには、1次元の添字配列変数と連想配列変数が用意されている。 declare組み込み関数は明示的に配列を宣言する。 配列のサイズに上限はなく、メンバにインデックスを付けたり連続的に代入したりする必要もない。 インデックス付き配列は整数(算術式を含む)で参照され、ゼロベースである。 特に断りのない限り、添字付き配列の添字は非負整数でなければならない。
      as an indexed array; the declare builtin will explicitly declare an array.  There is  no  maximum
      limit  on  the size of an array, nor any requirement that members be indexed or assigned contigu‐
      ously.  Indexed arrays are referenced using integers (including arithmetic expressions)  and  are
      zero-based;  associative  arrays are referenced using arbitrary strings.  Unless otherwise noted,
      indexed array indices must be non-negative integers.


      An indexed array is created automatically if  any  variable  is  assigned  to  using  the  syntax
添え字付き配列は、<nowiki>name[subscript]=value</nowiki> という構文で変数が代入されると自動的に作成される。 subscriptは算術式として扱われ、数値として評価されなければならない。 subscriptは無視される。
      name[subscript]=value.  The subscript is treated as an arithmetic expression that must evaluate
      to a number.  To explicitly declare an indexed array, use declare -a name (see SHELL BUILTIN COM‐
      MANDS below).  declare -a name[subscript] is also accepted; the subscript is ignored.


      Associative arrays are created using declare -A name.
連想配列は declare -a name で作成される。


      Attributes  may be specified for an array variable using the declare and readonly builtins.  Each
属性は、declare および readonly ビルトインを使用して配列変数に指定することができる。 各属性は、配列のすべてのメンバに適用される。
      attribute applies to all members of an array.


      Arrays are assigned to using compound assignments of the form  name=(value1 ... valuen),  where
配列の代入には、<nowiki>name=(value1 ... valuen)</nowiki>という形式の複合代入を使用する。 インデックス付き配列の代入では、文字列以外は必要ない。 リストの各値は、後述の [[#EXPANSION|EXPANSION ]] で説明するすべてのシェル展開を使って展開される。 subscript付き配列に代入する場合、オプションの括弧と添え字が指定されていれば、そのsubscriptが代入され、そうでなければ、代入される要素の添字は、文によって最後に代入されたsubscriptに1を加えたものになる。 インデックス付けはゼロから始まる。
      each  value may be of the form [subscript]=string.  Indexed array assignments do not require any‐
      thing but string.  Each value in the list is expanded using all the  shell  expansions  described
      below  under EXPANSION.  When assigning to indexed arrays, if the optional brackets and subscript
      are supplied, that index is assigned to; otherwise the index of the element assigned is the  last
      index assigned to by the statement plus one.  Indexing starts at zero.


      When  assigning to an associative array, the words in a compound assignment may be either assign‐
連想配列に代入する場合、複合代入の単語は、subscriptが必要な代入文か、キーと値を交互に並べたものとして解釈される単語のリスト、<nowiki>name=( key1 value1 key2 value2 ...)</nowiki> のいずれかになる。 これらは、<nowiki>name=( [key1]=value1 [key2]=value2 ...)</nowiki> と同様に扱われる。 リスト内のすべての代入は同じ型でなければならない。
      ment statements, for which the subscript is required, or a list of words that is interpreted as a
キーと値のペアを使う場合、キーがなかったり空だったりしてはならない。
      sequence  of alternating keys and values: name=( key1 value1 key2 value2 ...).  These are treated
      identically to name=( [key1]=value1 [key2]=value2 ...).  The first word in  the  list  determines
      how  the  remaining  words  are  interpreted; all assignments in a list must be of the same type.
      When using key/value pairs, the keys may not be missing  or  empty;  a  final  missing  value  is
      treated like the empty string.


      This  syntax  is also accepted by the declare builtin.  Individual array elements may be assigned
この構文はdeclare組み込み関数でも受け入れられる。 個々の配列要素には、上で紹介した <nowiki>name[subscript]=value</nowiki> 構文を使用して代入することができる。 subscript付き配列に代入する場合、name に負の数がsubscriptとして付くと、その数は name の最大subscriptより相対的に 1 大きいと解釈されるため、負のsubscriptは配列の末尾から数えていき、-1 のsubscriptは最後の要素を参照する。
      to using the name[subscript]=value syntax introduced above.  When assigning to an indexed  array,
      if  name is  subscripted  by  a  negative  number, that number is interpreted as relative to one
      greater than the maximum index of name, so negative indices count back from the end of the array,
      and an index of -1 references the last element.


      Any  element  of an array may be referenced using ${name[subscript]}.  The braces are required to
配列のどの要素も、<nowiki>${name[subscript]}</nowiki>を使って参照できる。 中括弧は、パス名展開との衝突を避けるために必要である。 subscriptが @ または * の場合、その単語は name のすべてのメンバに展開される。 これらのsubscriptは、単語が二重引用符で囲まれている場合のみ異なる。 単語が二重引用符で囲まれている場合、<nowiki>${name[*]}</nowiki> は各配列メンバの値を IFS 特殊変数の最初の文字で区切った 1 つの単語に展開され、<nowiki>${name[@]}</nowiki> は name の各要素を個別の単語に展開する。 配列メンバがない場合、<nowiki>${name[@]}</nowiki>は何も展開しない。 二重引用符による展開が単語内で行われる場合、最初のパラメータの展開は元の単語の先頭部分と結合され、最後のパラメータの展開は元の単語の最後の部分と結合される。 これは、特殊パラメータ*@(上記の[[#Special Parameters|Special Parameters ]]を参照)の展開に類似している。 の長さに展開される。  subscriptが * または @ の場合、展開されるのは配列の要素数である。 インデックス付き配列の要素を参照するために使用される添え字がゼロより小さい数として評価される場合、それは配列の最大インデックスより1つ大きいものに対する相対的なものとして解釈されるので、負のインデックスは配列の終わりからカウントバックし、-1のインデックスは最後の要素を参照する。
      avoid conflicts with pathname expansion.  If subscript is @ or *, the word expands to all members
      of  name.  These subscripts differ only when the word appears within double quotes. If the word
      is double-quoted, ${name[*]} expands to a single word with the value of each array  member  sepa‐
      rated  by the first character of the IFS special variable, and ${name[@]} expands each element of
      name to a separate word.  When there are no array members, ${name[@]} expands to nothing.  If the
      double-quoted expansion occurs within a word, the expansion of the first parameter is joined with
      the beginning part of the original word, and the expansion of the last parameter is  joined  with
      the last part of the original word.  This is analogous to the expansion of the special parameters
      * and @ (see Special Parameters above).  ${#name[subscript]} expands to the length of ${name[sub‐
      script]}.  If subscript is * or @, the expansion is the number of elements in the array.  If the
      subscript used to reference an element of an indexed array evaluates to a number less than  zero,
      it is interpreted as relative to one greater than the maximum index of the array, so negative in‐
      dices count back from the end of the array, and an index of -1 references the last element.


      Referencing an array variable without a subscript is equivalent to referencing the array  with  a
subscriptなしで配列変数を参照することは、subscript 0の配列を参照することと同じである。
      subscript of 0.  Any reference to a variable using a valid subscript is legal, and bash will cre‐
      ate an array if necessary.


      An array variable is considered set if a subscript has been assigned a value.  The null string is
subscriptに値が割り当てられている場合、配列変数は設定されていると見なされる。 NULL文字列は有効な値である。
      a valid value.


      It  is  possible to obtain the keys (indices) of an array as well as the values.  ${!name[@]} and
配列のキー(subscript)は、値と同様に取得できる。 <nowiki>${!name[@]} </nowiki>と<nowiki>${!name[*]}</nowiki>は、配列変数名に代入されたsubscriptに展開される。 二重引用符で囲んだ場合の扱いは、二重引用符で囲んだ場合の特殊パラメータ @ * の展開と同様である。
      ${!name[*]} expand to the indices assigned in array variable name.  The treatment when in  double
      quotes is similar to the expansion of the special parameters @ and * within double quotes.


      The unset builtin is used to destroy arrays.  unset name[subscript] destroys the array element at
<nowiki>unsetname[subscript]</nowiki>は、添え字の添え字にある配列要素を破棄する。 インデックス付き配列の負の添え字は、前述のように解釈される。 配列変数の最後の要素をアンセットしても、変数はアンセットされない。name が配列の場合は name をアンセットし、添え字が * または @ の場合は <nowiki>name[subscript]</nowiki> をアンセットすると、配列全体が削除される。
      index subscript, for both indexed and associative arrays.  Negative subscripts to indexed  arrays
      are interpreted as described above.  Unsetting the last element of an array variable does not un‐
      set the variable.  unset name, where name is an array, or unset name[subscript], where  subscript
      is * or @, removes the entire array.


      When  using  a  variable  name  with a subscript as an argument to a command, such as with unset,
unsetのように、subscript付きの変数名をコマンドの引数として使用する場合、上記の単語展開構文を使用しないと、引数はパス名展開の対象となる。 パス名展開を望まない場合は、引数を引用符で囲む必要がある。
      without using the word expansion syntax described above, the argument is subject to pathname  ex‐
      pansion.  If pathname expansion is not desired, the argument should be quoted.


      The declare, local, and readonly builtins each accept a -a option to specify an indexed array and
declare、local、readonlyの各ビルトインは、インデックス配列を指定する -aオプションと、連想配列を指定する -Aオプションを受け付ける。 両方のオプションが指定された場合は、-A が優先される。
      a -A option to specify an associative array.  If both options are supplied, -A takes  precedence.
read 組み込み関数には、標準入力から読み込んだ単語のリストを配列に代入する -a オプションが指定できる。 set および declare 組み込み関数は、配列の値を代入として再利用できるように表示する。
      The read builtin accepts a -a option to assign a list of words read from the standard input to an
      array.  The set and declare builtins display array values in a way that allows them to be  reused
      as assignments.


== EXPANSION ==
== EXPANSION ==