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

削除された内容 追加された内容
Fu7mu4 (トーク | 投稿記録)
Fu7mu4 (トーク | 投稿記録)
661 行
シェル関数呼び出しは、スクリプトやほとんどすべてのコマンドと同じように終了値を返します。終了値を明示的に指定するには、<tt>return</tt>コマンドを使用します。<tt>return</tt>コマンドにより、シェル関数の呼び出しを終了し、指定した終了値を返します。 (この目的で <tt>exit</tt>コマンドを使用できません。シェル関数の外部から呼び出された場合と同様にスクリプト全体を終了させます)終了値が指定されていない場合、つまり<tt>return</tt>コマンドに引数が指定されていないか<tt>return</tt>コマンドを実行せずに関数の終わりに到達すると、シェル関数は最後に実行されたコマンドの終了ステータスを返します。
 
加えて、シェル関数の宣言から<tt>function</tt>または<tt>( )</tt> を省略できますが、どちらかは残しておかなければなりません。多くのプログラマは<source lang="bash" enclose="none">function get_password ( )</source>と書く代わりに、<source lang="bash" enclose="none">get_password()</source>と書くことが多いようです。同様に、<tt>{ &hellip; }</tt>の表記は必須ではありませんし、これはシェル関数に限ることでもありません。<tt>{ &hellip; }</tt>は、一連のコマンドをグルーピング化してひとつの複合コマンドにまとめるものです。シェル関数の本体は複合コマンドでなければなりませんが複合コマンドは、<tt>{ &hellip; }</tt> 表記のブロックまたは<tt>if</tt>ステートメントです。本体が単一の複合コマンドしかなくて理論的には省略できる場合であっても、通常は<tt>{ &hellip; }</tt>ブロックです。
Incidentally, either <tt>function</tt> or <tt>( )</tt> may be omitted from a function declaration, but at least one must be present. Instead of <source lang="bash" enclose="none">function get_password ( )</source>, many programmers write <source lang="bash" enclose="none">get_password()</source>. Similarly, the <tt>{ &hellip; }</tt> notation is not exactly required, and is not specific to functions; it is simply a notation for grouping a sequence of commands into a single compound command. The body of a function must be a compound command, such as a <tt>{ &hellip; }</tt> block or an <tt>if</tt> statement; <tt>{ &hellip; }</tt> is the conventional choice, even when all it contains is a single compound command and so could theoretically be dispensed with.
 
==サブシェル、環境変数、スコープ==