<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja">
	<id>http://mochiuwiki.e2.valueserver.jp/index.php?action=history&amp;feed=atom&amp;title=%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%AE%E5%9F%BA%E7%A4%8E_-_%E9%96%A2%E6%95%B0</id>
	<title>シェルスクリプトの基礎 - 関数 - 版の履歴</title>
	<link rel="self" type="application/atom+xml" href="http://mochiuwiki.e2.valueserver.jp/index.php?action=history&amp;feed=atom&amp;title=%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%AE%E5%9F%BA%E7%A4%8E_-_%E9%96%A2%E6%95%B0"/>
	<link rel="alternate" type="text/html" href="http://mochiuwiki.e2.valueserver.jp/index.php?title=%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%AE%E5%9F%BA%E7%A4%8E_-_%E9%96%A2%E6%95%B0&amp;action=history"/>
	<updated>2026-09-02T10:21:06Z</updated>
	<subtitle>このウィキのこのページに関する変更履歴</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>http://mochiuwiki.e2.valueserver.jp/index.php?title=%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%AE%E5%9F%BA%E7%A4%8E_-_%E9%96%A2%E6%95%B0&amp;diff=11492&amp;oldid=prev</id>
		<title>Wiki: 文字列「__FORCETOC__」を「{{#seo:
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag…</title>
		<link rel="alternate" type="text/html" href="http://mochiuwiki.e2.valueserver.jp/index.php?title=%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%AE%E5%9F%BA%E7%A4%8E_-_%E9%96%A2%E6%95%B0&amp;diff=11492&amp;oldid=prev"/>
		<updated>2024-10-14T01:38:37Z</updated>

		<summary type="html">&lt;p&gt;文字列「__FORCETOC__」を「{{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag…&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ja&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← 古い版&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2024年10月14日 (月) 10:38時点における版&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l207&quot;&gt;207行目:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;207行目:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  &amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  &amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;{{#seo:&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;|image=/resources/assets/MochiuLogo_Single_Blue.png&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;__FORCETOC__&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;__FORCETOC__&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[カテゴリ:シェルスクリプト]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[カテゴリ:シェルスクリプト]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Wiki</name></author>
	</entry>
	<entry>
		<id>http://mochiuwiki.e2.valueserver.jp/index.php?title=%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%AE%E5%9F%BA%E7%A4%8E_-_%E9%96%A2%E6%95%B0&amp;diff=2549&amp;oldid=prev</id>
		<title>Wiki: ページの作成:「== 概要 ==  &lt;br&gt;&lt;br&gt;  == 関数の基本 == ==== 関数の定義 ==== 以下のいずれかの構文を利用して、関数を定義することができる。&lt;br&gt;  &lt;s…」</title>
		<link rel="alternate" type="text/html" href="http://mochiuwiki.e2.valueserver.jp/index.php?title=%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%AE%E5%9F%BA%E7%A4%8E_-_%E9%96%A2%E6%95%B0&amp;diff=2549&amp;oldid=prev"/>
		<updated>2020-11-27T17:34:48Z</updated>

		<summary type="html">&lt;p&gt;ページの作成:「== 概要 ==  &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;  == 関数の基本 == ==== 関数の定義 ==== 以下のいずれかの構文を利用して、関数を定義することができる。&amp;lt;br&amp;gt;  &amp;lt;s…」&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新規ページ&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== 概要 ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 関数の基本 ==&lt;br /&gt;
==== 関数の定義 ====&lt;br /&gt;
以下のいずれかの構文を利用して、関数を定義することができる。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
 function funcname&lt;br /&gt;
 {&lt;br /&gt;
    &amp;lt;shell commands...&amp;gt;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 または&lt;br /&gt;
 &lt;br /&gt;
 function funcname()&lt;br /&gt;
 {&lt;br /&gt;
    &amp;lt;shell commands...&amp;gt;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== 関数の削除 ====&lt;br /&gt;
定義されている関数を削除するには、以下のコマンドを実行する。&amp;lt;br&amp;gt;&lt;br /&gt;
 unset -f funcname&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
現在のログインセッションにおいて、どのような関数が定義されているかを確認するには、以下のコマンドを実行する。&amp;lt;br&amp;gt;&lt;br /&gt;
 declare -F    # 関数名のみ出力&lt;br /&gt;
 declare -f    # 関数の定義も出力&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== 関数と環境変数 ====&lt;br /&gt;
シェルスクリプトの関数は、実行したシェルと同じプロセスで実行される。&amp;lt;br&amp;gt;&lt;br /&gt;
したがって、以下のように、関数内で環境変数を設定した場合、関数を呼び出したシェルにも反映される。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
 function myfunc()&lt;br /&gt;
 {&lt;br /&gt;
    export AAA=100&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== 関数の定義の注意点 ====&lt;br /&gt;
関数の内容が無い場合、シンタックスエラーが出力される。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
 myfunc()&lt;br /&gt;
 {&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 # 出力&lt;br /&gt;
 ./Sample.sh: 行 3: 予期しないトークン `}&amp;#039; 周辺に構文エラーがあります&lt;br /&gt;
 ./Sample.sh: 行 3: `}&amp;#039;&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 関数にパラメータを渡す ==&lt;br /&gt;
関数に渡されたパラメータの処理方法は、シェルスクリプト実行時に渡されたコマンドラインパラメータの処理方法と同じである。&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== 関数へ渡されたパラメータの数を調べる($#) ====&lt;br /&gt;
関数内で$#を参照する場合、渡されたパラメータの数を調べることができる。&amp;lt;br&amp;gt;&lt;br /&gt;
また、関数の外で$#を参照する場合、シェルスクリプト実行時に渡されたコマンドラインパラメータの数を取得できる。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
 function SampleFunc()&lt;br /&gt;
 {&lt;br /&gt;
    echo $#&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 # 出力&lt;br /&gt;
 SampleFunc 100 200&lt;br /&gt;
 2&lt;br /&gt;
 &lt;br /&gt;
 SampleFunc aaa bbb ccc&lt;br /&gt;
 3&lt;br /&gt;
&lt;br /&gt;
 SampleFunc &amp;quot;This is a pen&amp;quot;&lt;br /&gt;
 1&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
以下の例では、関数へ渡されたパラメータが少なくとも1つ以上あるか確認している。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
 function SampleFunc()&lt;br /&gt;
 {&lt;br /&gt;
    if [ $# -lt 1 ]; then&lt;br /&gt;
       echo &amp;#039;Function &amp;quot;foo&amp;quot; needs at least one parameter&amp;#039; &amp;gt;&amp;amp;2&lt;br /&gt;
       exit -1&lt;br /&gt;
    fi&lt;br /&gt;
    echo &amp;#039;OK&amp;#039;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
以下の例では、&amp;lt;code&amp;gt;shift&amp;lt;/code&amp;gt;コマンドを使用して、関数へ渡されたパラメータを順番に処理している。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
 function SampleFunc()&lt;br /&gt;
 {&lt;br /&gt;
    while [ -n &amp;quot;$1&amp;quot; ]&lt;br /&gt;
    do&lt;br /&gt;
       echo $1&lt;br /&gt;
       shift&lt;br /&gt;
    done&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 # 出力&lt;br /&gt;
 SampleFunc 1 2 3&lt;br /&gt;
 1&lt;br /&gt;
 2&lt;br /&gt;
 3&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
また、パラメータをシングルクォーテーションあるいはダブルクォーテーションで括ると、1つのパラメータとして処理される。&amp;lt;br&amp;gt;&lt;br /&gt;
 # 出力&lt;br /&gt;
 SampleFunc &amp;quot;1 2 3&amp;quot;&lt;br /&gt;
 1 2 3&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
==== 関数のデフォルトパラメータの定義 ====&lt;br /&gt;
パラメータの参照において、&amp;lt;code&amp;gt;${1:-デフォルト値}&amp;lt;/code&amp;gt;という形で参照すると、&amp;lt;br&amp;gt;&lt;br /&gt;
1つ目のパラメータが指定されなかった場合、デフォルト値を使用することができる。&amp;lt;br&amp;gt;&lt;br /&gt;
(これは、シェルスクリプトのコマンドラインパラメータの扱いと同様である)&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
 function SampleFunc()&lt;br /&gt;
 {&lt;br /&gt;
    name=${1:-everyone}&lt;br /&gt;
    echo &amp;quot;Hello $name&amp;quot;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 # 出力&lt;br /&gt;
 SampleFunc&lt;br /&gt;
 Hello everyone&lt;br /&gt;
 &lt;br /&gt;
 SampleFunc Maku&lt;br /&gt;
 Hello Maku&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 関数のローカル変数 ==&lt;br /&gt;
シェルスクリプトは、関数の内部と関数の外部で変数を共有する。&amp;lt;br&amp;gt;&lt;br /&gt;
以下のように、関数内で代入を行う場合、関数外で代入した値が上書きされる。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
 function SampleFunc()&lt;br /&gt;
 {&lt;br /&gt;
    A=200&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 # 出力&lt;br /&gt;
 A=100&lt;br /&gt;
 SampleFunc&lt;br /&gt;
 echo $A&lt;br /&gt;
 &lt;br /&gt;
 200  # 変数Aの値が上書きされる&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
関数の内部のみ有効な変数を宣言する場合、localキーワードを付加する。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
 function SampleFunc()&lt;br /&gt;
 {&lt;br /&gt;
    local A=200&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 # 出力&lt;br /&gt;
 A=100&lt;br /&gt;
 SampleFunc&lt;br /&gt;
 echo $A&lt;br /&gt;
 &lt;br /&gt;
 100  # 変数Aの値は変化しない&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
以下の例では、ローカル変数にデフォルト引数を定義している。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
 function SampleFunc()&lt;br /&gt;
 {&lt;br /&gt;
    local A=${1:-ABC};&lt;br /&gt;
    echo &amp;quot;param = $A&amp;quot;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 # 出力&lt;br /&gt;
 SampleFunc&lt;br /&gt;
 param = ABC&lt;br /&gt;
&lt;br /&gt;
 SampleFunc 10000&lt;br /&gt;
 param = 10000&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 関数の戻り値 ==&lt;br /&gt;
関数の戻り値において、returnで返すことのできる値は数値(0～255)のみである。&amp;lt;br&amp;gt;&lt;br /&gt;
returnで返された戻り値を参照する場合、$?を参照する。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
 function SampleFunc()&lt;br /&gt;
 {&lt;br /&gt;
    return 1&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 # 出力&lt;br /&gt;
 SampleFunc&lt;br /&gt;
 echo $?&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
関数の実行結果を文字列として受け取る場合、関数の内部でechoした文字列をバッククォートを括った関数を実行して取得する。&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;syntaxhighlight lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
 function SampleFunc&lt;br /&gt;
 {&lt;br /&gt;
    echo &amp;#039;hoge&amp;#039;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 # 出力&lt;br /&gt;
 val=`SampleFunc`  または  val=$(SampleFunc)&lt;br /&gt;
 echo $val&lt;br /&gt;
 &lt;br /&gt;
 hoge&lt;br /&gt;
 &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__FORCETOC__&lt;br /&gt;
[[カテゴリ:シェルスクリプト]]&lt;/div&gt;</summary>
		<author><name>Wiki</name></author>
	</entry>
</feed>