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

第4章. 変数ヴァナキュラー

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

エラーメッセージや代入文の中に、${0##*/} という熟語が含まれているのをよく見かける。$0 を参照しているように見えるが、それ以上の何かが起こっているのだ。変数参照と、これらの特殊文字が何をするのかを詳しく見てみよう。ここで発見できるのは、いくつかの特殊文字でかなりの力を発揮する文字列操作の数々である。

変数リファレンス

参照 変数の値を参照するのは、ほとんどのプログラミング言語ではとても簡単だ。 変数の名前を使うか、その名前に文字を追加して、値を取得したいことを明示的に示すだけだ。 bashでもそうだ。VAR=something という名前で変数に代入し、という値の前にドル記号を付けて取得する。$VAR なぜドル記号が必要なのかと思うかもしれないが、bashは主に文字列を扱うので、ドル記号が必要なのだ:

MSG="Error: FILE not found"

は、表示された4つの単語の単純な文字列を与える:

MSG="Error: $FILE not found"

は、$FILE をその変数の値で置き換える(おそらく、探しているファイルの名前を保持しているのだろう)。

変数補間

この文字列置換を行いたい場合は、必ず二重引用符 を使うこと。 一重引用符を使うと、すべての文字が文字通りに解釈され、置換は起こらない。

変数名がどこで終わるかの混乱を避けるために(この例ではスペースがそれを容易にしている)、変数参照のより完全な構文は、変数名の周りに中括弧を使用する${FILE}

この構文、と中括弧は、変数参照に関する多くの特殊構文の基礎となっている。 例えば、変数名${#VAR} の前にハッシュ記号を置くと、その値ではなく、その値の文字列の長さを返すことができる。

VAR}である。 VAR}とする。

一言

7

/usr/bin/longpath.txt

21

多言語文字列

24

3

1

2356

4

1427685

7

しかし、bashは単に値やその長さ()を取得するだけではない。

パラメータ拡張

変数の値を取得するとき、特定の置換や編集を指定することができ、リターンされる値に影響を与えることができる(ただし、変数内の値には影響しない-1つのケースを除く)。 この構文には、変数名を区切るために使用される中括弧内の文字の特殊化が含まれる。${VAR##*/}. 以下に、このような展開について知っておく価値のあるものをいくつか挙げる。

ベースネームの省略形

スクリプトを呼び出すとき、スクリプトを呼び出すコマンドとして ファイル名だけを使うかもしれないが、これはスクリプトに実行変数があり、PATH 変数のディレクトリのいずれかにあることを前提とする。 ./scriptnameスクリプトが現在の作業ディレクトリにある場合は、フルパス名、/home/smith/utilities/scriptname 、または相対パス名でスクリプトを起動することもできる。

どちらの方法でスクリプトを起動した場合でも、$0 、スクリプトの起動に使用した文字シーケンス(相対パスか絶対パスか、どのように表現したかを問わない)が含まれる。

スクリプトの名前を使用状況メッセージに出力する場合、ファイル名(ベースネーム)だけが必要で、そこにたどり着くまでのパスは必要ないだろう: ...

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