「Transwiki:Bash Shell Scripting」の版間の差分

削除された内容 追加された内容
Fu7mu4 (トーク | 投稿記録)
Fu7mu4 (トーク | 投稿記録)
801 行
Bashの変数のスコープに不規則な点をいくつか見てきました。要約すると以下のようになります。
 
* 通常のBash変数の範囲は、それを含むシェル、またはそれに含まれるサブシェルに限定されています。
* Regular Bash variables are scoped to the shell that contains them, including any subshells in that shell.
** 通常のBash変数は子プロセスからは見えません。(外部プログラムからも)
** They are not visible to any child processes (that is, to external programs).
** サブシェルで作成された通常のBash変数は親のシェルからは見えません。
** If they are created inside a subshell, they are not visible to the parent shell.
** 通常のBash変数がサブシェル内で修正された場合、その変更は親のシェルからは見えません。
** If they are modified inside a subshell, those modifications are not visible to the parent shell.
** この挙動は関数でも同じで、通常のBash変数にもあてはまる。
** This is also true of functions, which in many ways are similar to regular Bash variables.
* Function-calls are not inherently run in subshells.
** 関数内での変数の修正は通常、関数を呼び出したコードから ''見え''ます。
** A variable modification within a function ''is'' generally visible to the code that calls the function.
* Bash variables that are exported into the environment are scoped to the shell that contains them, including any subshells ''or child processes'' in that shell.
** The <tt>export</tt> built-in command can be used to export a variable into the environment. (There are other ways as well, but this is the most common way.)