/usr/bin/bash: Difference between revisions

Line 667: Line 667:


=== Parameter Expansion ===
=== Parameter Expansion ===
      The  `$' character introduces parameter expansion, command substitution, or arithmetic expansion.
文字でパラメータ展開、コマンド置換、算術展開を行う。
      The parameter name or symbol to be expanded may be enclosed in braces,  which  are  optional  but
中括弧はオプションであるが、展開される変数を、名前の一部と解釈される可能性のある直後の文字から保護する役割を果たす。
      serve to protect the variable to be expanded from characters immediately following it which could
      be interpreted as part of the name.


      When braces are used, the matching ending brace is the first `}' not escaped by  a  backslash  or
中括弧が使われる場合、マッチする終端中括弧は、バックスラッシュでエスケープされていない最初の<nowiki>`}'</nowiki>であり、引用符で囲まれた文字列内でも、埋め込まれた算術展開、コマンド置換、パラメータ展開内でもない。
      within a quoted string, and not within an embedded arithmetic expansion, command substitution, or
      parameter expansion.


      ${parameter}
:<nowiki>${parameter}</nowiki>
              The value of parameter is substituted.  The braces are required when parameter is a  posi‐
::パラメータの値が置き換えられる。 中括弧が必要なのは、パラメータが1桁以上の位置パラメータである場合、またはパラメータの後に名前の一部として解釈されない文字が続く場合である。 パラメータは、前述の[[#PARAMETERS|PARAMETERS ]])または配列参照(Arrays)のシェル・パラメータである。
              tional  parameter  with  more than one digit, or when parameter is followed by a character
              which is not to be interpreted as part of its name.  The parameter is a shell parameter as
              described above PARAMETERS) or an array reference (Arrays).


      If  the first character of parameter is an exclamation point (!), and parameter is not a nameref,
パラメータの最初の文字が感嘆符(!)であり、パラメータがnamerefでない場合、間接参照レベルが導入される。 Bashは、パラメータの残りの部分を展開してできた値を新しいパラメータとして使用する。その後、この値が展開され、元のパラメータの展開ではなく、残りの展開で使用される。 これは間接展開として知られている。
      it introduces a level of indirection.  Bash uses the value formed by expanding the rest of param‐
この値は、チルダ展開、パラメータ展開、コマンド置換、算術展開の対象となる。 parameterがnamerefの場合、完全な間接展開ではなく、parameterによって参照されるパラメーターの名前に展開される。 例外は、後述の<nowiki>${!prefix*}</nowiki>と<nowiki>${!name[@]}</nowiki>の展開である。 感嘆符は、間接展開を行うために左中括弧の直後に置かなければならない。
      eter as the new parameter; this is then expanded and that value is used in the rest of the expan‐
      sion, rather than the expansion of the original parameter.  This is known as indirect  expansion.
      The  value  is  subject to tilde expansion, parameter expansion, command substitution, and arith‐
      metic expansion.  If parameter is a nameref, this expands to the name of the parameter referenced
      by  parameter  instead of performing the complete indirect expansion.  The exceptions to this are
      the expansions of ${!prefix*} and ${!name[@]} described below.  The exclamation point must  imme‐
      diately follow the left brace in order to introduce indirection.


      In each of the cases below, word is subject to tilde expansion, parameter expansion, command sub‐
以下の各ケースにおいて、wordはチルダ展開、パラメータ展開、コマンド置換、算術展開の対象となる。
      stitution, and arithmetic expansion.


      When not performing substring expansion, using the forms documented below (e.g., :-), bash  tests
部分文字列の展開を行わない場合、bashは以下で説明する形式(例::-)で、未設定またはNULLのパラメータをテストする。 コロンを省略すると、未設定のパラメータのみをテストすることになる。
      for a parameter that is unset or null.  Omitting the colon results in a test only for a parameter
      that is unset.


      ${parameter:-word}
:<nowiki>${parameter:-word}</nowiki>
              Use Default Values.  If parameter is unset or null, the expansion of word is  substituted.
::デフォルト値を使用する。 パラメータが未設定またはNULLの場合、wordの展開が代入される。それ以外の場合は、パラメータの値が代入される。
              Otherwise, the value of parameter is substituted.
:<nowiki>${parameter:=word}</nowiki>
      ${parameter:=word}
::デフォルト値を割り当てる。 パラメータが未設定または NULL の場合、word の展開がパラメータに代入される。 その後、パラメータの値が代入される。 位置パラメーターと特殊パラメーターは、この方法では代入できない。
              Assign  Default  Values.  If parameter is unset or null, the expansion of word is assigned
:<nowiki>${parameter:?word}</nowiki>
              to parameter.  The value of parameter is then substituted.  Positional parameters and spe‐
::Nullまたは未設定の場合、エラーを表示する。 パラメータがNULLまたは未設定の場合、wordの展開(wordが存在しない場合はその旨のメッセージ)が標準エラーに書き込まれ、シェルが対話型でない場合は終了する。 そうでなければ、パラメータの値が代入される。
              cial parameters may not be assigned to in this way.
:<nowiki>${parameter:+word}</nowiki>
      ${parameter:?word}
::代替値を使用する。 パラメータがNULLまたは未設定の場合、何も代入されない。
              Display  Error if Null or Unset.  If parameter is null or unset, the expansion of word (or
:<nowiki>${parameter:offset}</nowiki>
              a message to that effect if word is not present) is written to the standard error and  the
:<nowiki>${parameter:offset:length}</nowiki>
              shell, if it is not interactive, exits.  Otherwise, the value of parameter is substituted.
::サブストリング展開。 offsetで指定した文字から、パラメータ値の長さ分まで展開する。 パラメータが @ または * 添え字付き添字配列、連想配列名の場合、結果は以下のように異なる。 lengthが省略された場合、offsetで指定された文字から始まり、値の終わりまで続くパラメータ値の部分文字列に展開される。lengthとoffsetは算術式である(以下の[[#ARITHMETIC EVALUATION|ARITHMETIC EVALUATION ]]を参照)。
      ${parameter:+word}
              Use Alternate Value.  If parameter is null or unset, nothing is substituted, otherwise the
              expansion of word is substituted.
      ${parameter:offset}
      ${parameter:offset:length}
              Substring Expansion.  Expands to up to length characters of the value of parameter  start‐
              ing at the character specified by offset.  If parameter is @, an indexed array subscripted
              by @ or *, or an associative array name, the results differ as described below.  If length
              is  omitted,  expands to the substring of the value of parameter starting at the character
              specified by offset and extending to the end of the value.  length and offset  are  arith‐
              metic expressions (see ARITHMETIC EVALUATION below).


              If  offset evaluates to a number less than zero, the value is used as an offset in charac‐
::offsetが0より小さい数値と評価された場合、その値はパラメータ値の最後からの文字数分のオフセットとして使用される。 lengthが0より小さい数値と評価された場合、それは文字数ではなく、パラメータ値の最後からの文字数のオフセットとして解釈され、拡張はoffsetとその結果の間の文字となる。 負のオフセットは、:-展開と混同しないように、コロンから少なくとも1つのスペースで区切らなければならないことに注意。
              ters from the end of the value of parameter.  If length evaluates to a  number  less  than
              zero,  it is interpreted as an offset in characters from the end of the value of parameter
              rather than a number of characters, and the expansion is the characters between offset and
              that result.  Note that a negative offset must be separated from the colon by at least one
              space to avoid being confused with the :- expansion.


              If parameter is @, the result is length positional parameters beginning at offset.  A neg‐
::パラメータが@の場合、結果はoffsetから始まる長さの位置パラメータとなる。 負のオフセットは、最大の位置パラメーターよりも相対的に1大きい値として扱われるので、-1のオフセットは最後の位置パラメーターとして評価される。 lengthが0より小さい数に評価されると、展開エラーとなる。
              ative  offset  is taken relative to one greater than the greatest positional parameter, so
              an offset of -1 evaluates to the last positional parameter.  It is an expansion  error  if
              length evaluates to a number less than zero.


              If parameter is an indexed array name subscripted by @ or *, the result is the length mem‐
::パラメータが @ または * 添え字付きの添字配列名の場合、結果は <nowiki>${parameter[offset]}</nowiki>で始まる配列の長さメンバとなる。 負のオフセットは、指定された配列の最大添字より相対的に大きいものとなる。 lengthが0より小さい数に評価されると、展開エラーとなる。
              bers of the array beginning with ${parameter[offset]}.  A negative offset is  taken  rela‐
              tive to one greater than the maximum index of the specified array.  It is an expansion er‐
              ror if length evaluates to a number less than zero.


              Substring expansion applied to an associative array produces undefined results.
::連想配列にサブストリング展開を適用すると、未定義の結果が生じる。


              Substring indexing is zero-based unless the positional parameters are used, in which  case
::位置パラメーターが使われない限り、 サブ文字列のインデックスは0ベースである。 offsetが0で、位置パラメーターが使われている場合は、リストの先頭に$0が付く。
              the  indexing  starts  at 1 by default.  If offset is 0, and the positional parameters are
              used, $0 is prefixed to the list.


      ${!prefix*}
:<nowiki>${!prefix*}</nowiki>
      ${!prefix@}
:<nowiki>${!prefix@}</nowiki>
              Names matching prefix.  Expands to the names of variables whose names begin  with  prefix,
::プレフィックスに一致する名前。 プレフィックスで始まる変数名を、IFS特殊変数の最初の文字で区切って展開する。 が使われ、展開が二重引用符で囲まれている場合、各変数名は別々の単語に展開される。
              separated  by the first character of the IFS special variable.  When @ is used and the ex‐
              pansion appears within double quotes, each variable name expands to a separate word.


      ${!name[@]}
:<nowiki>${!name[@]}</nowiki>
      ${!name[*]}
:<nowiki>${!name[*]}</nowiki>
              List of array keys.  If name is an array variable, expands to the list  of  array  indices
::配列のキーのリスト。 nameが配列変数の場合、nameに代入された配列のインデックス(キー)のリストに展開される。 nameが配列でない場合、nameが設定されていれば0に展開され、そうでなければnullに展開される。 が使われ、展開が二重引用符で囲まれている場合、各キーは別々の単語に展開される。
              (keys)  assigned  in  name.  If name is not an array, expands to 0 if name is set and null
              otherwise.  When @ is used and the expansion appears within double quotes,  each  key  ex‐
              pands to a separate word.


      ${#parameter}
:<nowiki>${#parameter}</nowiki>
              Parameter  length.  The length in characters of the value of parameter is substituted.  If
::パラメータの長さ。 パラメータ値の文字数を代入する。 パラメータが*または@の場合、置換される値は位置パラメータの数である。 パラメータが * または @ を添字にした配列名の場合、置換される値は配列の要素数となる。 パラメータが負の数で添字付けされた配列名である場合、その数はパラメータの最大添字より1つ大きい相対数として解釈されるため、負の添字は配列の最後から数えていき、-1の添字は最後の要素を参照する。
              parameter is * or @, the value substituted is the number of positional parameters.  If pa‐
              rameter is an array name subscripted by * or @, the value substituted is the number of el‐
              ements in the array.  If parameter is an indexed array name subscripted by a negative num‐
              ber,  that  number is interpreted as relative to one greater than the maximum index of pa‐
              rameter, so negative indices count back from the end of the array, and an index of -1 ref‐
              erences the last element.


      ${parameter#word}
:<nowiki>${parameter#word}</nowiki>
      ${parameter##word}
:<nowiki>${parameter##word}</nowiki>
              Remove  matching  prefix  pattern.  The  word is expanded to produce a pattern just as in
::マッチする接頭辞パターンを取り除く。 この単語は、パス名展開と同じようにパターンを生成するために展開され、 以下のパターンマッチングのところで説明されているルールを使って、 展開されたパラメータの値とマッチングされる。 もしパターンがパラメー ターの値の先頭にマッチすれば、展開の結果は、最も短いパターン(#の場合)か、 最も長いパターン(###の場合)が削除された、展開されたパラメーター の値となる。 パラメータが@または*の場合、パターン除去操作は各位置のパラメータに順番に適用され、展開結果はリストとなる。 パラメータが@または*で添え字された配列変数の場合、パターン除去操作は配列の各メンバに順番に適用され、展開結果はリストとなる。
              pathname expansion, and matched against the expanded value of parameter  using  the  rules
              described under Pattern Matching below.  If the pattern matches the beginning of the value
              of parameter, then the result of the expansion is the expanded value of parameter with the
              shortest  matching  pattern (the  ``#'' case) or the longest matching pattern (the ``##''
              case) deleted.  If parameter is @ or *, the pattern removal operation is applied  to  each
              positional parameter in turn, and the expansion is the resultant list.  If parameter is an
              array variable subscripted with @ or *, the pattern removal operation is applied  to  each
              member of the array in turn, and the expansion is the resultant list.


      ${parameter%word}
:<nowiki>${parameter%word}</nowiki>
      ${parameter%%word}
:<nowiki>${parameter%%word}</nowiki>
              Remove matching  suffix  pattern.  The  word is expanded to produce a pattern just as in
::マッチする接尾辞パターンを取り除く。 この単語はパス名展開と同じようにパターンを生成するために展開され、 以下のパターンマッチングのところで説明されているルールを使って、 展開されたパラメータの値とマッチされる。 もしパターンが展開されたパラメータの値の末尾の部分にマッチした場合、 展開の結果は展開されたパラメータの値で、最も短いパターン(%の場合)または 最も長いパターン(%%の場合)が削除されたものになります。 パラメータが@または*の場合、パターン除去操作は各位置のパラメータに順番に適用され、展開結果はリストとなる。 パラメータが@または*で添え字された配列変数である場合、パターン除去操作は配列の各メンバに順番に適用され、展開結果はリストとなる。
              pathname expansion, and matched against the expanded value of parameter  using  the  rules
              described  under Pattern Matching below.  If the pattern matches a trailing portion of the
              expanded value of parameter, then the result of the expansion is the expanded value of pa‐
              rameter  with  the shortest matching pattern (the ``%'' case) or the longest matching pat‐
              tern (the ``%%'' case) deleted.  If parameter is @ or *, the pattern removal operation  is
              applied to each positional parameter in turn, and the expansion is the resultant list.  If
              parameter is an array variable subscripted with @ or *, the pattern removal  operation  is
              applied to each member of the array in turn, and the expansion is the resultant list.


      ${parameter/pattern/string}
:<nowiki>${parameter/pattern/string}</nowiki>
              Pattern  substitution.  The pattern is expanded to produce a pattern just as in pathname
::パターンの置換。 パス名展開と同じようにパターンが展開され、Parameterが展開され、 パターンとその値との最長一致が文字列に置き換えられる。 マッチは、後述のパターンマッチングで説明するルールで実行される。 patternが/で始まる場合、patternのすべてのマッチが文字列に置き換えられる。通常、最初にマッチしたものだけが置き換えられる。 patternが#で始まる場合、パラメータの展開値の先頭にマッチしなければならない。 patternが%で始まる場合、展開されたパラメータ値の末尾にマッチしなければならない。 文字列がNULLの場合、パターンのマッチは削除され、パターンに続く/は省略される。 nocasematchシェルオプションが有効な場合、アルファベットの大文字小文字を区別せずにマッチが実行される。 パラメータが@または*の場合、置換操作は各位置パラメータに順番に適用され、展開結果はリストとなる。 パラメータが@または*で添え字された配列変数の場合、置換操作は配列の各メンバに順番に適用され、展開結果はリストとなる。
              expansion, Parameter is expanded and the longest match of pattern against its value is re‐
              placed with string.  The match is performed using the rules described under Pattern Match‐
              ing below.  If pattern begins with /, all matches of pattern  are  replaced  with  string.
              Normally only the first match is replaced.  If pattern begins with #, it must match at the
              beginning of the expanded value of parameter.  If pattern begins with %, it must match  at
              the  end  of  the  expanded value of parameter.  If string is null, matches of pattern are
              deleted and the / following pattern may be omitted.  If the nocasematch  shell  option  is
              enabled,  the  match is performed without regard to the case of alphabetic characters.  If
              parameter is @ or *, the substitution operation is applied to each positional parameter in
              turn,  and  the  expansion  is the resultant list.  If parameter is an array variable sub‐
              scripted with @ or *, the substitution operation is applied to each member of the array in
              turn, and the expansion is the resultant list.


      ${parameter^pattern}
:<nowiki>${parameter^pattern}</nowiki>
      ${parameter^^pattern}
:<nowiki>${parameter^^pattern}</nowiki>
      ${parameter,pattern}
:<nowiki>${parameter,pattern}</nowiki>
      ${parameter,,pattern}
:<nowiki>${parameter,,pattern}</nowiki>
              Case modification.  This expansion modifies the case of alphabetic characters in parame‐
::大文字と小文字を区別する。 この展開では、パラメータ中のアルファベットの大文字小文字を修正する。 パス名展開と同様に、パターンが展開される。 展開されたパラメータ値の各文字は、パターンと照合され、パターンにマッチする場合、大文字小文字が変換される。 パターンは、複数の文字にマッチしてはならない。 演算子 ^ はパターンにマッチする小文字を大文字に変換し、演算子 , はマッチする大文字を小文字に変換する。 と ,, の展開は、展開された値のマッチした各文字を変換する。 ^ , の展開は、展開された値の最初の文字にのみマッチして変換する。 patternが省略された場合は、すべての文字にマッチするパラメータが@または*の場合、大文字小文字の修正操作は各位置パラメータに順番に 適用され、展開結果はリストとなる。 パラメータが@または*で添え字された配列変数である場合、大文字小文字の変更操作は配列の各メンバに順番に適用され、展開結果はリストとなる。
              ter.  The pattern is expanded to produce a pattern just as in  pathname  expansion.  Each
              character in the expanded value of parameter is tested against pattern, and, if it matches
              the pattern, its case is converted.  The pattern should not attempt to match more than one
              character. The ^ operator converts lowercase letters matching pattern to uppercase; the ,
              operator converts matching uppercase letters to lowercase.  The ^^ and ,, expansions  con‐
              vert  each  matched character in the expanded value; the ^ and , expansions match and con‐
              vert only the first character in the expanded value.  If pattern is omitted, it is treated
              like  a  ?, which matches every character.  If parameter is @ or *, the case modification
              operation is applied to each positional parameter in turn, and the expansion is the resul‐
              tant  list.  If parameter is an array variable subscripted with @ or *, the case modifica‐
              tion operation is applied to each member of the array in turn, and the  expansion  is  the
              resultant list.


      ${parameter@operator}
:<nowiki>${parameter@operator}</nowiki>
              Parameter transformation.  The expansion is either a transformation of the value of param‐
::パラメータの変換。 演算子の値によって、パラメータ値の変換か、パラメータそのものの情報が展開される。 各演算子は1文字である:
              eter or information about parameter itself, depending on the value of operator.  Each  op‐
              erator is a single letter:


              U     The  expansion is a string that is the value of parameter with lowercase alphabetic
::U&nbsp;&nbsp;展開は、小文字のアルファベットを大文字に変換したパラメータ値の文字列である。
                    characters converted to uppercase.
::u&nbsp;&nbsp;拡張は、パラメータの値がアルファベットの場合、最初の文字を大文字に変換した文字列である。
              u     The expansion is a string that is the value of parameter with the  first  character
::L&nbsp;&nbsp;展開は、大文字のアルファベットを小文字に変換したパラメータ値の文字列である。
                    converted to uppercase, if it is alphabetic.
::Q&nbsp;&nbsp;展開は、入力として再利用できる形式で引用されたパラメータの値である文字列である。
              L     The  expansion is a string that is the value of parameter with uppercase alphabetic
::E&nbsp;&nbsp;展開は、<nowiki>$'...'</nowiki>クォーテーションの仕組みと同様にバックスラッシュエスケープシーケンスを展開した文字列で、パラメータの値である。
                    characters converted to lowercase.
::P&nbsp;&nbsp;この展開は、パラメータ値をプロンプト文字列のように展開した結果の文字列である(下記の[[#PROMPTING|PROMPTING ]]を参照)。
              Q     The expansion is a string that is the value of parameter quoted in  a  format  that
::A&nbsp;&nbsp;この展開文は、代入文または宣言コマンドの形をした文字列で、評価されると、パラメータをその属性と値で再作成する。
                    can be reused as input.
::K&nbsp;&nbsp;パラメータ値の引用符で囲まれた可能性のあるバージョンを出力する。ただし、インデックス付き配列と連想配列の値は、引用符で囲まれたキーと値のペアのシーケンスとして出力される(上記の[[#Arrays|Arrays ]]を参照)。
              E     The  expansion is a string that is the value of parameter with backslash escape se‐
::a&nbsp;&nbsp;展開は、パラメータの属性を表すフラグ値からなる文字列である。
                    quences expanded as with the $'...' quoting mechanism.
              P     The expansion is a string that is the result of expanding the value of parameter as
                    if it were a prompt string (see PROMPTING below).
              A     The expansion is a string in the form of an assignment statement or declare command
                    that, if evaluated, will recreate parameter with its attributes and value.
              K     Produces a possibly-quoted version of the value of parameter, except that it prints
                    the  values  of  indexed  and  associative arrays as a sequence of quoted key-value
                    pairs (see Arrays above).
              a      The expansion is a string consisting of flag values  representing  parameter's  at‐
                    tributes.


              If parameter is @ or *, the operation is applied to each positional parameter in turn, and
パラメータが@または*の場合、操作は各位置パラメータに順番に適用され、展開結果はリストとなる。 パラメータが@または*で添え字された配列変数の場合、操作は配列の各メンバに順番に適用され、展開結果はリストとなる。
              the expansion is the resultant list.  If parameter is an array variable subscripted with @
              or  *,  the operation is applied to each member of the array in turn, and the expansion is
              the resultant list.


              The result of the expansion is subject to word splitting and  pathname  expansion  as  de‐
展開結果は、後述の単語分割とパス名展開の対象となる。
              scribed below.


=== Command Substitution ===
=== Command Substitution ===