Linux‎ > ‎

シェル - echoで文字に色をつける その3


目次


シェル - echoで文字に色をつける その1」では、多くの端末で採用されている基本的な属性を示しました。しかしもともとの規格ECMA-48には既にご説明した内容よりもたくさんの設定が存在します。

例えば…
  • 「3」イタリック
  • 「6」遅い点滅
  • 「10-19」フォント変更
  • 「21」二重下線
  • 「22」太字・強調の解除
  • 「23」イタリックの解除
  • 「24」下線の解除
  • 「25」点滅の解除
  • 「27」反転の解除
  • 「39」文字色の解除
  • 「49」背景色の解除

特定の属性のみ解除するものが目立ちます。「背景色や下線は解除せずに文字色のみを解除したい」などという強い希望があるのなら「39」を指定することもいいと思います。しかし一部の属性だけの解除は、何を解除して何が効いているのか分かりにくくするコードになるためお勧めできません。色属性の変わり目では「\e[m」を使用することをお勧めします。


Standard ECMA-48の61,62ページ(実ページでは75,76ページ)に記述があります。全量を以下に示します。
0 default rendition (implementation-defined), cancels the effect of any preceding occurrence of SGR in the data stream regardless of the setting of the GRAPHIC RENDITION COMBINATION MODE (GRCM)
1 bold or increased intensity
2 faint, decreased intensity or second colour
3 italicized
4 singly underlined
5 slowly blinking (less then 150 per minute)
6 rapidly blinking (150 per minute or more)
7 negative image
8 concealed characters
9 crossed-out (characters still legible but marked as to be deleted)
10 primary (default) font
11 first alternative font
12 second alternative font
13 third alternative font
14 fourth alternative font
15 fifth alternative font
16 sixth alternative font
17 seventh alternative font
18 eighth alternative font
19 ninth alternative font
20 Fraktur (Gothic)
21 doubly underlined
22 normal colour or normal intensity (neither bold nor faint)
23 not italicized, not fraktur
24 not underlined (neither singly nor doubly)
25 steady (not blinking)
26 (reserved for proportional spacing as specified in CCITT Recommendation T.61)
27 positive image
28 revealed characters
29 not crossed out
30 black display
31 red display
32 green display
33 yellow display
34 blue display
35 magenta display
36 cyan display
37 white display
38 (reserved for future standardization; intended for setting character foreground colour as specified in ISO 8613-6 [CCITT Recommendation T.416])
39 default display colour (implementation-defined)
40 black background
41 red background
42 green background
43 yellow background
44 blue background
45 magenta background
46 cyan background
47 white background
48 (reserved for future standardization; intended for setting character background colour as specified in ISO 8613-6 [CCITT Recommendation T.416])
49 default background colour (implementation-defined)
50 (reserved for cancelling the effect of the rendering aspect established by parameter value 26)
51 framed
52 encircled
53 overlined
54 not framed, not encircled
55 not overlined
56 (reserved for future standardization)
57 (reserved for future standardization)
58 (reserved for future standardization)
59 (reserved for future standardization)
60 ideogram underline or right side line
61 ideogram double underline or double line on the right side
62 ideogram overline or left side line
63 ideogram double overline or double line on the left side
64 ideogram stress marking
65 cancels the effect of the rendition aspects established by parameter values 60 to 64

余談

エスケープ文字の表現について「\033」よりも「\e」をお勧めすると書きました。しかしネット上のサンプルでは「\033」を使用しているサイトを多く見かけます。「\e」が使えない環境があるのか、あえて「\033」を使う理由があるのか、ただ単に理解せずにパクリあっているからなのかよくわかりません。特別な理由がある場合には教えていただきたいです。

ネット上のサンプルでは、属性リセットとして「\033[0;39m」を使用しているサイトを複数見かけました。このページをお読みの方ならすでにお気づきのことと思いますが、全属性リセットの指定と共に文字色オフを指定していることになり、実質的に無意味(のハズ)です。パクリ拡散により広まった間違った記述であると思われます。真似しない様にしましょう。

また、カラーコードを以下のように説明しているサイトもありました。
Black       0;30     Dark Gray     1;30
Blue        0;34     Light Blue    1;34
Green       0;32     Light Green   1;32
Cyan        0;36     Light Cyan    1;36
Red         0;31     Light Red     1;31
Purple      0;35     Light Purple  1;35
Brown       0;33     Yellow        1;33
Light Gray  0;37     White         1;37
色を明るくする場合に「1;」を付加し、そうでない場合には「0;」を付加するかのような記述。「1」はBoldの意味であり色を明るくするという意味ではありません。結果的にそうなる端末もあると思いますが、根本的な意味合いは異なります。

特に「0」を毎回つけているのは無意味でありかつ誤解を与えます。また、「1」はあくまでも付加属性であるため、「30;1」のように文字色指定の後に記述するほうが自然だと思います。正直なところ私が最初に参照したサイトはこのような説明になっており、サンプルを読むのにも非常に苦労してしまいました…分かりやすいサイトが増えてくれるといいなぁと思います。


参考URL

2011/06/09