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 イディオム

第1章 大きな "if "イディオム

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

bashのイディオムを理解する手始めとして、if/then/else のような構文で通常できることを、より簡潔な構文でできるようにするbashの構文を見てみよう。 この章で紹介するイディオム式には、実際の利点(主に長所)と避けるべき落とし穴がある。 しかし、bashのイディオムを知らないと、何が起こっているのか分からなかったり、気づかなかったりするかもしれない。

このコードを見てほしい:

[[ -n "$DIR" ]] && cd "$DIR"

これがif 文に見えるだろうか? bashを使いこなしている人なら、これは機能的には同じものだとわかるだろう。if というキーワードがコードに出てこなくても、if 文だと理解できるはずだ。

どうしたんだ?

大きな "もしも"

この慣用句を説明するために、まず似ているがもっと簡単な例を見てみよう:

cd tmp && rm scratchfile

これも事実上、if の文である。 cd コマンドが成功したら、(そしてそのときだけ)rm コマンドを実行する。 ここでの "慣用句 "は、通常 "and "と読み取られるダブルアンパサンド(&& )を使って2つのコマンドを区切ることである。

論理学や哲学の授業では、「A AND B」という式は、AとBがそれぞれ真である場合にのみ真である、という規則を教える。 例えば、"I own a dog AND I own a cat "を考えてみよう。 もし私が犬を飼っていなければ、猫の状況にかかわらず、この複合式は私にとって偽である。

これをbashに当てはめてみよう。 bashの基本的な機能はプログラムの実行であることを覚えておいてほしい。 この文の最初の部分には、cd コマンドが実行されている。 ANDの論理と同様に、この最初のコマンドが失敗した場合、bashは2番目の部分、rm コマンドを実行しようとしない。

&& を使っているのは、ANDの振る舞いを思い出させるためだ。bashはこの2つの結果に対して実際に「AND」演算子を行っているわけではない。 (これがC/C++だったら、条件付き実行は同じだが、話は別だろう)。 しかし、このbashイディオムは2番目のコマンドの条件付き実行を提供する。

元の例、つまりこの式に戻って見てみよう:

[[ -n "$DIR" ]] && cd "$DIR"

これで「わかった」と思うだろうか? 最初の式は、DIR という変数の値の長さがゼロでないかどうかをテストしている。 この変数に値がある場合、つまり長さがゼロでない場合、そしてゼロでない場合のみ、cd コマンドは、DIR の値で名前付けされたディレクトリに変更しようとする。

これを明示的にif

if [[ -n "$DIR" ]]; then
    cd "$DIR"
fi

bashにあまり詳しくない人にとっては、後者の形式の方が読みやすく、理解しやすいのは確かだ。 しかし、then 節の中では、cd コマンドがあるだけで、あまり多くのことが行われていないので、構文が少し「かさばる」感じがする。 読者がどのような人たちなのか、また、then 節の中に他のコマンドが追加される可能性がどの程度あるのかを考慮して、どちらを使うかを決める必要がある。このトピックについては、 のセクションで詳しく説明する。

bashヘルプ ...

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