|
|
Line 999: |
Line 999: |
|
| |
|
| == FUNCTIONS == | | == FUNCTIONS == |
| A shell function, defined as described above under SHELL GRAMMAR, stores a series of commands for
| | シェル関数は、上記の''SHELL GRAMMAR''で説明したように定義され、後で実行するための一連のコマンドを格納する。 シェル関数の名前が単純なコマンド名として使用されると、その関数名に関連付けられたコマンドのリストが実行される。 関数は現在のシェルのコンテキストで実行され、関数を解釈するために新しいプロセスが作成されることはない(シェルスクリプトの実行とは対照的である)。 関数が実行されると、関数の引数は実行中の位置パラメーターとなる。 特殊パラメータ#は、変更を反映して更新される。 特別パラメータ0は変更されない。 FUNCNAME変数の最初の要素は、関数の実行中に関数名に設定される。 |
| later execution. When the name of a shell function is used as a simple command name, the list of
| |
| commands associated with that function name is executed. Functions are executed in the context
| |
| of the current shell; no new process is created to interpret them (contrast this with the execu‐
| |
| tion of a shell script). When a function is executed, the arguments to the function become the
| |
| positional parameters during its execution. The special parameter # is updated to reflect the
| |
| change. Special parameter 0 is unchanged. The first element of the FUNCNAME variable is set to
| |
| the name of the function while the function is executing.
| |
|
| |
|
| All other aspects of the shell execution environment are identical between a function and its
| | シェル実行環境の他のすべての側面は、これらの例外を除いて、関数とその呼び出し元との間で同一である: DEBUG トラップおよび RETURN トラップ(後述の [[#SHELL BUILTIN COMMANDS|SHELL BUILTIN COMMANDS ]] にある trap 組み込み関数の説明を参照)は、関数に trace 属性が与えられているか(後述の declare 組み込み関数の説明を参照)、set 組み込み関数で -o functrace シェル・オプションが有効になっていない限り継承されず(この場合、すべての関数は DEBUG トラップおよび RETURN トラップを継承する)、ERR トラップは -o errtrace シェル・オプションが有効になっていない限り継承されない。 |
| caller with these exceptions: the DEBUG and RETURN traps (see the description of the trap builtin
| |
| under SHELL BUILTIN COMMANDS below) are not inherited unless the function has been given the
| |
| trace attribute (see the description of the declare builtin below) or the -o functrace shell op‐
| |
| tion has been enabled with the set builtin (in which case all functions inherit the DEBUG and RE‐
| |
| TURN traps), and the ERR trap is not inherited unless the -o errtrace shell option has been en‐
| |
| abled.
| |
|
| |
|
| Variables local to the function may be declared with the local builtin command. Ordinarily,
| | 関数にローカルな変数は、local builtin コマンドで宣言することができる。 通常、変数とその値は、関数とその呼び出し元との間で共有される。 ローカル変数が宣言された場合、その変数の可視スコープはその関数とその子関数(その関数が呼び出す関数を含む)に限定される。 ローカル変数は、以前のスコープで宣言された同名の変数を「シャドウ」する。 例えば、関数内で宣言されたローカル変数は、同名のグローバル変数を隠蔽する。参照と代入はローカル変数を参照し、グローバル変数は変更されないままである。 関数が戻ると、グローバル変数は再び見えるようになる。 |
| variables and their values are shared between the function and its caller. If a variable is de‐
| |
| clared local, the variable's visible scope is restricted to that function and its children (in‐
| |
| cluding the functions it calls). Local variables "shadow" variables with the same name declared
| |
| at previous scopes. For instance, a local variable declared in a function hides a global vari‐
| |
| able of the same name: references and assignments refer to the local variable, leaving the global
| |
| variable unmodified. When the function returns, the global variable is once again visible.
| |
|
| |
|
| The shell uses dynamic scoping to control a variable's visibility within functions. With dynamic
| | シェルは動的スコープを使用して、関数内の変数の可視性を制御する。 動的スコープでは、可視変数とその値は、実行が現在の関数に到達する原因となった一連の関数呼び出しの結果である。 関数が目にする変数の値は、呼び出し元が「グローバル」スコープであるか、別のシェル関数であるかにかかわらず、呼び出し元での値に依存する。 これは、ローカル変数宣言が''shadow''する値でもあり、関数が戻ったときに復元される値でもある。 |
| scoping, visible variables and their values are a result of the sequence of function calls that
| |
| caused execution to reach the current function. The value of a variable that a function sees de‐
| |
| pends on its value within its caller, if any, whether that caller is the "global" scope or an‐
| |
| other shell function. This is also the value that a local variable declaration "shadows", and
| |
| the value that is restored when the function returns.
| |
|
| |
|
| For example, if a variable var is declared as local in function func1, and func1 calls another
| | 例えば、ある変数varが関数func1内でローカル変数として宣言され、func1が別の関数func2を呼び出した場合、func2内からのvarへの参照はfunc1のローカル変数varに解決され、varという名前のグローバル変数はshadowされる。 |
| function func2, references to var made from within func2 will resolve to the local variable var
| |
| from func1, shadowing any global variable named var.
| |
|
| |
|
| The unset builtin also acts using the same dynamic scope: if a variable is local to the current
| | unset組み込み関数も、同じダイナミック・スコープを使用して動作する。変数が現在のスコープにローカルな場合、unsetはそれをアンセットする。そうでない場合、unsetは上記のように、呼び出し元のスコープで見つかった変数を参照する。 現在のローカルスコープにある変数がアンセットされた場合、そのスコープでリセットされるか、関数がリターンするまでアンセットのままである。 関数が戻ると、以前のスコープにあったその変数のインスタンスが見えるようになる。 アンセットが前のスコープにある変数に作用した場合、シャドウされていたその名前の変数のインスタンスはすべて見えるようになる。 |
| scope, unset will unset it; otherwise the unset will refer to the variable found in any calling
| |
| scope as described above. If a variable at the current local scope is unset, it will remain so
| |
| until it is reset in that scope or until the function returns. Once the function returns, any
| |
| instance of the variable at a previous scope will become visible. If the unset acts on a vari‐
| |
| able at a previous scope, any instance of a variable with that name that had been shadowed will
| |
| become visible.
| |
|
| |
|
| The FUNCNEST variable, if set to a numeric value greater than 0, defines a maximum function nest‐
| | FUNCNEST変数に0以上の数値を設定すると、関数の最大入れ子レベルが定義される。 この制限を超えた関数呼び出しは、コマンド全体を中断させる。 |
| ing level. Function invocations that exceed the limit cause the entire command to abort.
| |
|
| |
|
| If the builtin command return is executed in a function, the function completes and execution re‐
| | 組み込みコマンドreturnが関数内で実行された場合、関数は完了し、関数呼び出しの次のコマンドで実行が再開される。 RETURNトラップに関連付けられたコマンドは、実行が再開される前に実行される。 関数が完了すると、位置パラメータと特殊パラメータ#の値は、関数実行前の値に戻される。 |
| sumes with the next command after the function call. Any command associated with the RETURN trap
| |
| is executed before execution resumes. When a function completes, the values of the positional
| |
| parameters and the special parameter # are restored to the values they had prior to the func‐
| |
| tion's execution.
| |
|
| |
|
| Function names and definitions may be listed with the -f option to the declare or typeset builtin
| | 関数名と定義は、declareまたはtypeset組み込みコマンドの-fオプションでリストすることができる。 declareまたはtypesetの-Fオプションは、関数名のみをリストアップする(extdebugシェル・オプションが有効な場合は、オプションでソース・ファイルと行番号もリストアップされる)。 関数は、export 組み込み関数に -f オプションを付けることで、サブシェルに自動的に定義されるようにエクスポートできる。 関数の定義を削除するには、unset 組み込み関数の -f オプションを使用する。 |
| commands. The -F option to declare or typeset will list the function names only (and optionally
| |
| the source file and line number, if the extdebug shell option is enabled). Functions may be ex‐
| |
| ported so that subshells automatically have them defined with the -f option to the export
| |
| builtin. A function definition may be deleted using the -f option to the unset builtin.
| |
|
| |
|
| Functions may be recursive. The FUNCNEST variable may be used to limit the depth of the function
| | 関数は再帰的に使用できる。 FUNCNEST 変数を使用して、関数呼び出しスタックの深さを制限し、関数の呼び出し回数を制限することができる。 デフォルトでは、再帰呼び出しの回数に制限はない。 |
| call stack and restrict the number of function invocations. By default, no limit is imposed on
| |
| the number of recursive calls.
| |
|
| |
|
| == ARITHMETIC EVALUATION == | | == ARITHMETIC EVALUATION == |