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

M
</source>
 
prints this出力内容: <pre>bar&#xA;bar&#xA;bip&#xA;foo&#xA;&#xA;bar</pre>
 
{{Transwiki:Bash Shell Scripting/tip|変数の値を一時的に変更する関数が必要な場合、-- 呼び出し中は修正した値を使用し呼び出し後に元の値に戻したい場合 -- その関数呼び出しを丸括弧でくくることができます。これをサブシェルといいます。サブシェルは修正を ''隔離'' しそのまわりの実行環境へ影響を波及させません。(可能な場合、この方法で記述した関数は問題が起きにくいです。また <tt>local</tt> キーワードも助けてくれます。)}}
{{Transwiki:Bash Shell Scripting/tip|If you need to call a function that modifies one or more variables, but you don't actually want those variables to be modified, you can wrap the function call in parentheses, so it takes place in a subshell. This will "isolate" the modifications and prevent them from affecting the surrounding execution environment. (That said: when possible, it's better to write functions in such a way that this problem doesn't arise to begin with. As we'll see soon, the <tt>local</tt> keyword can help with this.)}}
 
The same is true of function definitions; just like a regular variable, a function defined within a subshell is not visible outside the subshell.
112

回編集