Bash で echo '\t' とかやっても \t とそのまま出力されるだけでタブ文字にならなくて、うーんとか言いながら printf に書き換えるというのをずっとやっていたのだけど、さっき echo $'\t' と書くとタブ文字が出力されることを初めて知った。色々調べていくと結局 man を見ましょうということになって面白い。
man bash を見ると、 QUOTING の節にこう書いてあって、ドル記号とシングルクオートを使って $'string' と書くと、 string の部分で、 ANSI C のバックスラッシュでのエスケープシーケンスを展開して置き換えてくれるということだった、なるほど助かった。
QUOTING
(中略)
Words of the form $'string' are treated specially. The word expands
to string, with backslash-escaped characters replaced as specified by
the ANSI C standard. Backslash escape sequences, if present, are
decoded as follows:
(中略)
A double-quoted string preceded by a dollar sign ($"string") will
cause the string to be translated according to the current locale.
If the current locale is C or POSIX, the dollar sign is ignored. If
the string is translated and replaced, the replacement is double-
quoted.
https://www.man7.org/linux/man-pages/man1/bash.1.html#QUOTING
ちなみに、ドル記号とダブルクオート $"string" はまた違う意味を持っていて、いまの locale によって変換とか置換とかをやりますということが書かれている。 locale が何なのかちゃんと理解してなかったけどこことかを見ると国などによってテキストの表示含めた様々なものをうまくやるための設定のようだった。
wiki.archlinux.jp
ちなみに(2)、 printf '\t' にするとちゃんとタブ文字で出力されていたのは、 printf が C のエスケープシーケンスを展開するからであるというのも man printf を見るとわかった。
man7.org
ちなみに(3)、 man printf とやって見れるマニュアルは printf(1) なのだけど、この数字はセクションとのことで、数字ごとに意味を分けているというのが man man を見るとわかった。 1 はユーザコマンドで 3 は C のライブラリ関数というようなことになっていそうで、例えば printf コマンドとライブラリ関数としての printf それぞれのマニュアルが分かれている。それぞれを見るには man 1 printf とか man 3 printf とかやると見られる。 man -k printf とかやると存在するマニュアルが全部出る (apropos コマンドというのと同じ挙動をするらしい、 apropos は「折よく」「それはそうと」みたいな意味のようなので「ところでこれなんだっけ」みたいなコマンドなのかな……)。言われてみると確かに crontab のファイルの書き方を知りたくて man crontab したけど crontab(1) には書いてなくて crontab(5) を見たことがある気がする。数字を覚えていると launchd(8) とか書かれたときにシステム管理者ツール/デーモン系か〜とか分かって便利そうだけど数字を覚えるのが大変そう。
1 Executable programs or shell commands
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions, e.g. /etc/passwd
6 Games
7 Miscellaneous (including macro packages and conventions), e.g.
man(7), groff(7)
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]
https://man7.org/linux/man-pages/man1/man.1.html
ちなみに(4)、 ANSI C が何なのかもちゃんと理解していなかったけどこことかを見ると C 言語の標準のひとつということ? のようだった。
blog.ansi.org