/usr/bin/bash: Difference between revisions

Line 168: Line 168:


=== Compound Commands ===
=== Compound Commands ===
      A compound command is one of the following.  In most cases a list in a command's description  may
複合コマンドは以下のいずれかである。 ほとんどの場合、コマンドの説明のリストは、コマンドの残りの部分と1つ以上の改行で区切られ、セミコロンの代わりに改行が続くことがある。
      be  separated from the rest of the command by one or more newlines, and may be followed by a new‐
      line in place of a semicolon.


      (list) list is executed in a subshell environment (see COMMAND  EXECUTION  ENVIRONMENT below).
;(list)  
              Variable  assignments  and builtin commands that affect the shell's environment do not re‐
:リストはサブシェル環境で実行される(後述の「[[#COMMAND EXECUTION ENVIRONMENT|COMMAND  EXECUTION  ENVIRONMENT]]」を参照)。
              main in effect after the command completes.  The return status is the exit status of list.
シェルの環境に影響を与える変数割り当てや組み込みコマンドは、コマンドの完了後には有効にならない。 リターン・ステータスは list の終了ステータスである。


      { list; }
;<nowiki>{ list; }</nowiki>
              list is simply executed in the current shell environment.  list must be terminated with  a
:listは単に現在のシェル環境で実行される。listは改行またはセミコロンで終了しなければならない。 これはグループ・コマンドとして知られている。 戻りステータスは、listの終了ステータスである。 メタキャラクタ (and)とは異なり、<nowiki>{and}</nowiki>は予約語であり、予約語を認識することが許可されている場所に出現しなければならないことに注意すること。 これらは単語の区切りを引き起こさないので、空白または他のシェル・メタキャラクタでlistと区切らなければならない。
              newline  or semicolon.  This is known as a group command.  The return status is the exit
              status of list.  Note that unlike the metacharacters ( and ), { and } are  reserved  words
              and  must  occur  where  a reserved word is permitted to be recognized.  Since they do not
              cause a word break, they must be separated  from  list  by  whitespace  or  another  shell
              metacharacter.


      ((expression))
;((expression))
              The expression is evaluated according to the rules described below under ARITHMETIC EVALU‐
:式は、後述の「[[#ARITHMETIC EVALUATION|ARITHMETIC EVALUATION]]」のルールに従って評価される。 式の値が0でない場合、戻り値は0であり、そうでない場合、戻り値は1である。
              ATION.  If the value of the expression is non-zero, the return status is 0; otherwise  the
              return status is 1.  This is exactly equivalent to let "expression".


      [[ expression ]]
;<nowiki>[[ expression ]]</nowiki>
              Return  a  status  of 0 or 1 depending on the evaluation of the conditional expression ex‐
:条件式の評価に応じて 0 または 1 のステータスを返す。 式は、後述の「[[#CONDITIONAL EXPRESSIONS|CONDITIONAL EXPRESSIONS]]」で説明するプライマリで構成される。  
              pression.  Expressions are composed of the primaries described below under CONDITIONAL EX‐
<nowiki>[[ and ]]</nowiki>の間の単語では、単語分割とパス名展開は行われない。
              PRESSIONS.  Word splitting and pathname expansion are not performed on the words between
チルダ展開、パラメータ展開、変数展開、算術展開、コマンド置換、プロセス置換、引用符除去は行われる。 -fのような条件演算子は、プライマリとして認識されるためには、引用符で囲まれていな ければならない。
              the [[ and ]]; tilde expansion, parameter and variable  expansion,  arithmetic  expansion,
              command  substitution, process substitution, and quote removal are performed.  Conditional
              operators such as -f must be unquoted to be recognized as primaries.


              When used with [[, the < and > operators sort lexicographically using the current locale.
:<nowiki>[[</nowiki>と一緒に使用すると、<nowiki>< and ></nowiki> 演算子は、現在のロケールを使用して辞書順にソートする。


      See the description of the test builtin command (section SHELL BUILTIN COMMANDS below)  for  the
:パラメータの扱い(パラメータの欠落など)については、test組み込みコマンドの説明(後述の「[[#SHELL BUILTIN COMMANDS|SHELL BUILTIN COMMANDS]]」セクション)を参照のこと。
      handling of parameters (i.e.  missing parameters).


      When  the  == and != operators are used, the string to the right of the operator is considered a
:演算子 <nowiki>==</nowiki> および <nowiki>!=</nowiki> が使用されると、演算子の右側の文字列はパターンとみなされ、 extglob シェル・オプションが有効であるかのように、後述の「パターン・マッチング」の規則に従ってマッチングされる。 nocasematchシェルオプションが有効な場合、アルファベットの大文字小文字を区別せずにマッチングが行われる。
      pattern and matched according to the rules described below under Pattern Matching, as if the ext‐
戻り値は、文字列がパターンにマッチする<nowiki>(==)</nowiki>かマッチしない<nowiki>(!=)</nowiki>なら0、そうでなければ1である。 パターンの任意の部分を引用符で囲むと、引用符で囲んだ部分が文字列としてマッチする。
      glob  shell  option  were enabled.  The = operator is equivalent to ==.  If the nocasematch shell
      option is enabled, the match is performed without regard to the case  of  alphabetic  characters.
      The return value is 0 if the string matches (==) or does not match (!=) the pattern, and 1 other‐
      wise.  Any part of the pattern may be quoted to force the quoted  portion  to  be  matched  as  a
      string.


      An  additional binary operator, =~, is available, with the same precedence as == and !=.  When it
:さらに二項演算子<nowiki>=~</nowiki>が使用でき、優先順位は<nowiki>==</nowiki>や<nowiki>!=</nowiki>と同じである。
      is used, the string to the right of the operator is considered a POSIX extended  regular  expres‐
:この演算子が使用されると、演算子の右側の文字列は POSIX 拡張正規表現とみなされ、 ('''[https://manpages.debian.org/bookworm/manpages-dev/regex.3.en.html regex]'''(3) で通常説明されている POSIX regcomp と regexec インターフェイスを使用して) それに従ってマッチングされる。 返り値は、文字列がパターンにマッチすれば 0、マッチしなければ 1 である。 正規表現が構文的に正しくない場合、条件式の戻り値は2である。
      sion and matched accordingly (using the POSIX regcomp and regexec interfaces usually described in
nocasematchシェルオプションが有効な場合、アルファベットの大文字小文字を区別せずにマッチが行われる。 パターンのどの部分も引用符で囲むことができ、引用符で囲んだ部分を文字列としてマッチさせることができる。 正規表現中の括弧表現は注意深く扱わなければならない。 なぜなら、通常のクォート文字は括弧の間で意味を失うからである。 パターンがシェル変数に格納されている場合、変数展開を引用符で囲むと、 パターン全体が文字列としてマッチするようになる。
      regex(3)).  The return value is 0 if the string matches the pattern, and  otherwise.  If  the
      regular  expression  is  syntactically incorrect, the conditional expression's return value is 2.
      If the nocasematch shell option is enabled, the match is performed without regard to the case of
      alphabetic  characters.  Any part of the pattern may be quoted to force the quoted portion to be
      matched as a string.  Bracket expressions in regular expressions must be treated carefully, since
      normal  quoting  characters  lose their meanings between brackets.  If the pattern is stored in a
      shell variable, quoting the variable expansion forces the entire  pattern  to  be  matched  as  a
      string.


      The  pattern will match if it matches any part of the string.  Anchor the pattern using the ^ and
:パターンは、文字列のどの部分にもマッチする。 正規表現演算子 ^ $ を使ってパターンにアンカーを付けると、文字列全体にマッチするようになる。 配列変数'''BASH_REMATCH'''は、文字列のどの部分がパターンにマッチしたかを記録する。 インデックス0の'''BASH_REMATCH'''の要素には、正規表現全体にマッチした文字列の部分が含まれる。 正規表現内の括弧で囲まれた部分式でマッチした文字列は、残りの'''BASH_REMATCH'''のインデックスに保存される。インデックスnの'''BASH_REMATCH'''の要素は、n番目の括弧付き部分式にマッチする文字列の部分である。
      $ regular expression operators to force it to match  the  entire  string.  The  array  variable
      BASH_REMATCH records which parts of the string matched the pattern.  The element of BASH_REMATCH
      with index 0 contains the portion of the string matching the entire  regular  expression.  Sub‐
      strings  matched  by  parenthesized subexpressions within the regular expression are saved in the
      remaining BASH_REMATCH indices. The element of BASH_REMATCH with index n is the  portion  of  the
      string matching the nth parenthesized subexpression.


      Expressions  may  be combined using the following operators, listed in decreasing order of prece‐
式は以下の演算子を使って結合することができる:
      dence:


              ( expression )
;( expression )
                    Returns the value of expression.  This may be used to override  the  normal  prece‐
:式の値を返す。 これは、演算子の通常の優先順位を上書きするために使われる。
                    dence of operators.
;! expression
              ! expression
:式が偽なら真。
                    True if expression is false.
;expression1 && expression2
              expression1 && expression2
:expression1とexpression2の両方が真なら真。
                    True if both expression1 and expression2 are true.
;expression1 || expression2
              expression1 || expression2
:expression1またはexpression2のどちらかが真なら真。
                    True if either expression1 or expression2 is true.


              The  && and || operators do not evaluate expression2 if the value of expression1 is suffi‐
:expression1の値が条件式全体の戻り値を決定するのに十分である場合、&&と<nowiki>||</nowiki>演算子はexpression2を評価しない。
              cient to determine the return value of the entire conditional expression.


      for name [ [ in [ word ... ] ] ; ] do list ; done
;<nowiki>for name [ [ in [ word ... ] ] ; ] do list ; done</nowiki>
              The list of words following in is expanded, generating a list of items.  The variable name
:inに続く単語のリストが展開され、項目のリストが生成される。 変数名はこのリストの各要素に順番に設定され、その都度listが実行される。 inが省略された場合、forコマンドは設定された位置パラメーター(後述の[[#PARAMETERS|PARAMETERS]]を参照)ごとにlistを1回実行する。 戻りステータスは、最後に実行されたコマンドの終了ステータスである。 inに続く項目の展開の結果、リストが空になった場合、コマンドは実行されず、リターン・ステータスは0となる。
              is  set  to  each element of this list in turn, and list is executed each time.  If the in
              word is omitted, the for command executes list once for each positional parameter that  is
              set (see PARAMETERS below).  The return status is the exit status of the last command that
              executes.  If the expansion of the items following in results in an empty  list,  no  com‐
              mands are executed, and the return status is 0.


      for (( expr1 ; expr2 ; expr3 )) ; do list ; done
;for (( expr1 ; expr2 ; expr3 )) ; do list ; done
              First, the arithmetic expression expr1 is evaluated according to the rules described below
:まず、算術式 expr1 は、後述の「[[#ARITHMETIC EVALUATION|ARITHMETIC EVALUATION]]」のルールに従って評価される。
              under ARITHMETIC EVALUATION.  The arithmetic expression expr2 is then evaluated repeatedly
算術式expr2は、ゼロになるまで繰り返し評価される。 expr2がゼロ以外の値に評価されるたびに、listが実行され、算術式expr3が評価される。 いずれかの式が省略された場合、その式は1に評価されるものとして動作する。 戻り値は、実行されたリストの最後のコマンドの終了ステータスであり、いずれかの式が無効な場合は偽である。
              until  it  evaluates to zero.  Each time expr2 evaluates to a non-zero value, list is exe‐
              cuted and the arithmetic expression expr3 is evaluated.  If any expression is omitted,  it
              behaves  as if it evaluates to 1.  The return value is the exit status of the last command
              in list that is executed, or false if any of the expressions is invalid.


      select name [ in word ] ; do list ; done
;select name <nowiki>[ in word ]</nowiki> ; do list ; done
              The list of words following in is expanded, generating a list of items.  The  set  of  ex‐
:inに続く単語のリストが展開され、項目のリストが生成される。 展開された単語のセットは標準エラーに出力される。 inが省略された場合、位置パラメーターが表示される(後述の[[#PARAMETERS|PARAMETERS]]を参照)。 次にPS3プロンプトが表示され、標準入力から1行が読み込まれる。 その行が、表示された単語の1つに対応する数字で構成されている場合、nameの値はその単語に設定される。 行が空の場合、単語とプロンプトが再度表示される。 EOFが読み込まれると、コマンドは完了する。 それ以外の値が読み込まれると、nameにはnullが設定される。 読み込まれた行は変数REPLYに保存される。 リストは、breakコマンドが実行されるまで、選択のたびに実行される。 selectの終了ステータスは、リスト内で最後に実行されたコマンドの終了ステータスであり、コマンドが実行されなかった場合は0である。
              panded  words is printed on the standard error, each preceded by a number.  If the in word
              is omitted, the positional parameters are printed (see PARAMETERS below).  The PS3  prompt
              is then displayed and a line read from the standard input. If the line consists of a num‐
              ber corresponding to one of the displayed words, then the value of name  is  set  to  that
              word.  If  the  line is empty, the words and prompt are displayed again.  If EOF is read,
              the command completes.  Any other value read causes name to be set to null.  The line read
              is  saved  in the variable REPLY.  The list is executed after each selection until a break
              command is executed.  The exit status of select is the exit status of the last command ex‐
              ecuted in list, or zero if no commands were executed.


      case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
;<nowiki>case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac</nowiki>
              A case command first expands word, and tries to match it against each pattern in turn, us‐
:caseコマンドはまず単語を展開し、後述のパターンマッチングで説明されているマッチングルールを使って、順番に各パターンとのマッチングを試みる。 単語は、チルダ展開、パラメータと変数の展開、算術展開、コマンド置換、プロセス置換、引用符除去を使って展開される。 検査される各パターンは、チルダ展開、パラメータ展開、変数展開、算術展開、コマンド置換、プロセス置換を使って展開される。 nocasematchシェル・オプションを有効にすると、アルファベットの大文字と小文字を区別せずにマッチが行われる。 マッチすると、対応するリストが実行される。 演算子;;が使われると、最初のパターンマッチの後、それ以降のマッチは試みられない。 の代わりに;&を使うと、次のパターン・セットに関連するリストが実行される。 の代わりに ;;& を使用すると、シェルは文中に次のパターンリストがあればそれをテストし、マッチした場合に関連するリストを実行する。 パターンにマッチしなければ、終了ステータスは0である。 それ以外の場合は、リスト内で最後に実行されたコマンドの終了ステータスとなる。
              ing the matching rules described under Pattern Matching below.  The word is expanded using
              tilde expansion, parameter and variable expansion, arithmetic expansion, command substitu‐
              tion, process substitution and quote removal.  Each pattern  examined  is  expanded  using
              tilde expansion, parameter and variable expansion, arithmetic expansion, command substitu‐
              tion, and process substitution.  If the nocasematch shell option is enabled, the match  is
              performed without regard to the case of alphabetic characters.  When a match is found, the
              corresponding list is executed.  If the ;; operator is used, no subsequent matches are at‐
              tempted  after  the first pattern match.  Using ;& in place of ;; causes execution to con‐
              tinue with the list associated with the next set of patterns.  Using ;;& in  place  of  ;;
              causes  the  shell to test the next pattern list in the statement, if any, and execute any
              associated list on a successful match, continuing the case statement execution as  if  the
              pattern  list had not matched.  The exit status is zero if no pattern matches.  Otherwise,
              it is the exit status of the last command executed in list.


      if list; then list; [ elif list; then list; ] ... [ else list; ] fi
;<nowiki>if list; then list; [ elif list; then list; ] ... [ else list; ] fi</nowi>
              The if list is executed.  If its exit status is zero, the then list is executed.  Other‐
:ifリストが実行される。 その終了ステータスがゼロであれば、thenリストが実行される。 そうでなければ、各elifリストが順番に実行され、その終了ステータスがゼロであれば、対応するthenリストが実行され、コマンドは完了する。 そうでない場合、elseリストがあればそれが実行される。 終了ステータスは、最後に実行されたコマンドの終了ステータスであり、どの条件も真をテストしなかった場合はゼロである。
              wise, each elif list is executed in turn, and if its exit status is zero, the correspond‐
              ing then list is executed and the command completes.  Otherwise, the  else  list  is  exe‐
              cuted,  if  present.  The exit status is the exit status of the last command executed, or
              zero if no condition tested true.


      while list-1; do list-2; done
;while list-1; do list-2; done
      until list-1; do list-2; done
;until list-1; do list-2; done
              The while command continuously executes the list list-2 as long as the last command in the
:whileコマンドは、リストlist-1の最後のコマンドがゼロの終了ステータスを返す限り、リストlist-2を実行し続ける。 untilコマンドはwhileコマンドと同じであるが、テストが否定される点が異なる: list-1の最後のコマンドが0以外の終了ステータスを返す限り、list-2は実行される。 whileコマンドとuntilコマンドの終了ステータスは、リスト-2で最後に実行されたコマンドの終了ステータスであり、何も実行されなかった場合はゼロである。
              list  list-1  returns an exit status of zero.  The until command is identical to the while
              command, except that the test is negated: list-2 is executed as long as the  last  command
              in list-1 returns a non-zero exit status.  The exit status of the while and until commands
              is the exit status of the last command executed in list-2, or zero if none was executed.


=== Coprocesses ===
=== Coprocesses ===