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

第5章 式と算術式

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

bashには、同じことを行うためのさまざまな方法があり、まったく異なることを行うためのほとんど同じ構文もある。 多くの場合、それはいくつかの特殊文字の違いにすぎない。${VAR}${#VAR} はすでに見たが、最初の式は変数の値を返すが、2番目はその文字列の長さを返す(「変数の参照」)。 また、${VAR[@]}${VAR[*]} の引用符の違いもある(「引用符とスペース」)

角括弧を2つ使うべきか、1つだけ使うべきか、あるいは使わないべきか。 (( ... )) 」と「$(( ... )) 」の違いは何だろう? 通常、記号にはさまざまな使い方に共通する意味があり、それが構文の背後にある理由のようなものを示唆している。 歴史的な理由からこのような表現が選ばれることもある。 これらの慣用的なパターンや算術式について説明できるかどうか、見てみよう。

整数のみ

bashシェルは整数演算しか使わない。その主な用途は、反復計算、ファイル点数、バイト単位でのサイズなどのカウントである。 浮動小数点数の計算が必要な場合はどうすればいいのだろうか?結局のところ、sleepコマンドは小数値を使えるようになった。sleep 0.25 は1/4秒スリープする。1/4秒の倍数スリープしたい場合はどうすればいいのだろうか?sleep $(( 6 * 0.25 )) と書きたいところだが、それはうまくいかない。

最も簡単な解決策は、bcawk のような別のプログラミングを使って計算を行うことである。例えば、fp というスクリプトがあるので、~/bin ディレクトリか、PATH のどこかに置くことができる(そして実行権限を与える):

# /bin/bash -
# fp - provide floating point, via awk
# usage:  fp  "expression"

awk "BEGIN { print $* }"

これがあれば、sleep $(fp "6 * 0.25") 、目的の浮動小数点計算を行うことができる。計算を行うのはbashではないかもしれないが、計算を行うのを手助けしてくれるのはbashなのだ。

算術

bashは主に文字列指向の言語だが、bashで二重括弧を見かけるときはいつも、文字列ではなく整数で算術評価が行われていることを意味している。 これは、二重括弧を使ったfor ループのバリエーションでおなじみだ:

for ((i=0; i<size; i++))

二重括弧の中の変数名の前に$ を使う必要がないことに注目してほしい。bashで二重括弧を使うときはいつもそうだ。では、二重括弧が使われている場所は他にあるのだろうか?

まず、ドル記号と二重括弧を使って算術計算を行い、シェル変数の値を作成する:

max=$(( intro + body + outro - 1 ))
median_loc=$((len / 2))

繰り返しになるが、変数が二重括弧の中で使われている場合、その前にドル記号を参照する必要がないことに注目してほしい。

次に、この二重括弧の使い方を考えてみよう:

if (( max - 3 > x * 4 )) ; then
    # Do something here
fi

今回はドル記号を先頭に付けずにダブルペアレンを使用する。 なぜか?何が違うのか?

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