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

第2章. ループ用語

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

Cスタイルのfor ループだけではない。bashには他の構文やスタイルもある。Pythonプログラマにはもっと馴染みのあるものもあるが、それぞれに使い道がある。 for 、引数のないループがあり、スクリプトでも関数の中でも役に立つ。 イテレータのようなfor ループもあり、明示的な値や他のコマンドから得られる値がある。

ループの構文

ループ コンストラクトはプログラミング言語では一般的である。 C言語が発明されて以来、多くのプログラミング言語がCスタイルのfor ループを採用している。 ループは、初期化コード、終了条件、反復コードをすべて1つの場所にまとめるため、非常に強力で読みやすい構成となっている。 例えば、C言語(あるいはJava、...)では、次のようになる:

/* NOT bash */
for (i=0; i<10; i++) {
    printf("%d\n", i);
}

ほんのわずかな構文の違いがあるだけで、bashもほとんど同じアプローチをとっている:

for ((i=0; i<10; i++)); do
    printf '%d\n' "$i"
done

特に、 、二重括弧を使っていることに注意されたい。 bashは中括弧ではなく、dodone を使ってループの文を囲んでいる。 C/C++と同様、for ループの慣用的な使い方は、空のfor ループであり、意図的な無限ループを与えている(while true; do も参照):

for ((;;)); do
    printf 'forever'
done

しかし、bashのfor ループの種類はそれだけではない。 シェルスクリプトでよく使われるイディオムを紹介しよう:

for value; do
    echo "$value"
    # Do more stuff with $value...
done

何かが欠けているように見えるが? value はどこで値を取得しているのだろう? コマンドライン上では何の役にも立たないが、Shellコマンドでこれを使うと、for のループがスクリプトのパラメータを繰り返し処理する。 つまり、$1 、次に$2 、次に$3 、というように、value の値として使用される。

このfor ループをmyloop.sh というファイルに置き、このように実行すると、3つの引数("-c"、"17"、"core")が出力される:

$ bash myloop.sh -c 17 core
-c
17
core
$

この省略されたfor ループは、関数定義の中で 、非常に頻繁に発見される:

function Listem {
    for arg; do
        echo "arg to func: '$arg'"
    done
    echo "Inside func: \$0 is still: '$0'"
}

関数定義内部では、$1$2 などのパラメータは関数に対するパラメータであり、シェルスクリプトに対するパラメータではない。 したがって、関数定義内部では、for のループが関数に渡されたパラメータを繰り返し処理する。

この ミニマリストfor ループは、スクリプトか関数のどちらかに渡されたパラメータを、暗黙のリストとして繰り返し処理する。スクリプトのメイン関数内で使用すると、スクリプトに渡されたパラメータを繰り返し処理し、シェル関数の内部で使用すると、その関数に渡されたパラメータを繰り返し処理する。 ...

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