Skip to Content
bash イディオム
book

bash イディオム

by Carl Albing, JP Vossen
May 2025
Intermediate to advanced
170 pages
2h 21m
Japanese
O'Reilly Media, Inc.
Content preview from bash イディオム

第6章. 機能的フレームワーク

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

前の2つの章では、変数を使い、その変数を組み合わせて式にするbashのイディオムについて説明してきた。 次の段階は、それらの式や文をbashシェルスクリプト内のさまざまな場所から呼び出し可能な関数にグループ化することである。 bashは関数という便利な構造をサポートしているとはいえ、それは非常にbash的な方法だ。 他の言語との違いを見てみよう。

関数呼び出し

以下は、 、bash関数を呼び出す例である3つの文である(この例のために作っている):

Do_Something
Find_File  25 $MPATH $ECODE
Show_All $*

関数呼び出しのようには見えない、と思うかもしれない。 コマンドラインでのコマンド呼び出しと同じように見えるのだ。

その通りだ。

他の言語では、Find_File(25, MPATH, ECODE) のように言うかもしれないが、それはbashではない。bashでは、関数は他のコマンドと同じように呼び出される。コマンドやシェルスクリプトを呼び出すように関数を呼び出すのだ。bashのキーワードや組み込み関数と同様に、シェルは関数を実行するために別のプロセスを作成する必要はない。 そのため、関数は個別のコマンド・バイナリやShellスクリプトを呼び出すよりも効率的である。

また、これらの関数呼び出しは、別の変数に代入できる値を返していないことに気づくかもしれない。 まずは、関数とそのパラメータの定義を見てみよう。

関数の定義

bashの構文には、いくつかのオプション要素が含まれている。例えば、helper という関数を定義したいとしよう。 その定義を始めるための3つの異なる方法がある:

function Helper ()
function Helper
Helper ()

この3つはすべて等価である。予約語function は省略可能であるが、もし使用するのであれば、括弧は省略可能である。私たちは、function と関数の名前のシンプルな2語バージョンを使用することを好む。何をしているのかを明確に示すだけでなく、他の言語とは異なり、関数のオプション・パラメータは括弧に入れられないことを思い出させてくれる。 また、grep

関数本体は、通常中括弧で囲まれた関数名の定義に従う:

function Say_So {
   echo 'Here we are in the function named Say_So'
}

関数パラメータ

、bash関数のパラメータはどのように定義されているのだろうか? そうではない。代わりに、bash関数(以下の段落で説明する)を呼び出す際には、好きなだけパラメータを指定することができる。一般的には、ある固定のパラメータ・セットを念頭に置き、それに従って関数を記述することになるが、引数の数を可変にするために必要な特殊化は何もない。

引数の数がいくつであっても、bashスクリプト自体の引数に使われるのと同じ構文を使って、$1$2$3 、といったように関数で使用できる。これらの引数には名前はなく、順番の番号しかないので、関数の先頭にコメントを付けて、関数がどのような引数をどのような順番で期待するかを文書化するのは非常に良いアイデアである。 また、スクリプトの最初の数行で、これらの位置パラメータがより説明的な名前の変数に割り当てられているのを見るのはよくあることである。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Bash クックブック第2版

Bash クックブック第2版

Carl Albing, JP Vossen
UXデザインの法則 第2版 ―最高のプロダクトとサービスを支える心理学

UXデザインの法則 第2版 ―最高のプロダクトとサービスを支える心理学

Jon Yablonski, 相島 雅樹, 磯谷 拓也, 反中 望, 松村 草也

Publisher Resources

ISBN: 9798341651357