/usr/bin/bash: Difference between revisions

Line 244: Line 244:


=== Shell Function Definitions ===
=== Shell Function Definitions ===
      A  shell  function is an object that is called like a simple command and executes a compound com‐
シェル関数は、単純なコマンドのように呼び出され、新しい位置パラメーターのセットを持つ複合コマンドを実行するオブジェクトである。 シェル関数は以下のように宣言される:
      mand with a new set of positional parameters.  Shell functions are declared as follows:


      fname () compound-command [redirection]
;<nowiki>fname () compound-command [redirection]</nowiki>
      function fname [()] compound-command [redirection]
;<nowiki>function fname [()] compound-command [redirection]</nowiki>
              This defines a function named fname.  The reserved word  function is  optional.  If  the
これはfnameという名前の関数を定義する。 予約語のfunctionは省略可能である。 関数の予約語を指定した場合、括弧は省略可能である。 関数の本体は複合コマンドcompound-commandである(上記の複合コマンドを参照のこと)。 このコマンドは通常{}の間のコマンドのリストであるが、1つの例外を除いて、上記の「複合コマンド」にリストされているコマンドであれば何でもよい: compound-commandは、fnameが単純コマンドの名前として指定されると、必ず実行される。 posixモードでは、fnameは有効なシェル名でなければならず、POSIX特殊組み込み関数の名前であってはならない。 デフォルトモードでは、関数名は$を含まない引用符で囲まれていないシェル語であれば何でもよい。 関数定義時に指定されたリダイレクション(後述の[[#REDIRECTION|REDIRECTION]]を参照)は、関数実行時に実行される。 構文エラーが発生するか、同じ名前の読み取り専用関数が既に存在しない限り、関数定義の終了ステータスは0である。 実行されると、関数の終了ステータスは、ボディ内で最後に実行されたコマンドの終了ステータスになる。 (後述の[[#FUNCTIONSを|FUNCTIONS]]を参照のこと)。
              function  reserved  word is supplied, the parentheses are optional.  The body of the func‐
              tion is the compound command compound-command (see Compound Commands above).  That command
              is  usually  a  list of commands between { and }, but may be any command listed under Com‐
              pound Commands above, with one exception: If the function reserved word is used,  but  the
              parentheses are not supplied, the braces are required.  compound-command is executed when‐
              ever fname is specified as the name of a simple command.  When in posix mode,  fname  must
              be  a  valid  shell name and may not be the name of one of the POSIX special builtins.  In
              default mode, a function name can be any unquoted shell word that does not contain $.  Any
              redirections  (see  REDIRECTION below) specified when a function is defined are performed
              when the function is executed.  The exit status of a function definition is zero unless  a
              syntax  error  occurs or a readonly function with the same name already exists.  When exe‐
              cuted, the exit status of a function is the exit status of the last  command  executed  in
              the body.  (See FUNCTIONS below.)


== COMMENTS ==
== COMMENTS ==