てきとうなメモ

本の感想とか技術メモとか

extquote

bashで$'string'と入力すると、文字列の内部のANSI Cライクなエスケープシーケンスを解釈してくれるという機能がある。

で、こちらの本の中でそれはextquoteと呼ぶと書いてあってあったので、呼び名があるんだと思ったのだが。

シェルプログラミング実用テクニック

シェルプログラミング実用テクニック

また、bashの機能(extquoteというもの)を使う方法もあるということでした
(以下$'\t'を使う話が続く)

bashのmanには

extquote
If set, $'string' and $"string" quoting is performed within ${parameter} expansions enclosed in double quotes. This option is enabled by default.

とあって、extquoteはこんな感じで、${parameter}の中でも$'string'や$"string"を展開するためのオプションの呼び名のようだ。

$ shopt -u extquote
$ echo $'ab\ncd'
ab
cd
$ echo "${v:-$'ab\ncd'}"
$'ab\ncd'
$ shopt -s extquote
$ echo $'ab\ncd'
ab
cd
$ echo "${v:-$'ab\ncd'}"
ab
cd

$'string'の機能自体はこのオプションによらず利用できている。

$'string'に関しては

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.

と、特に名前は付けられていない。

bashソースコード的にはansiexpandという関数を呼んでいるので、ansiexpandと呼んだ方が分かりやすいし適切な気がする。まあ、細かい話だけども。