[[diary/Kojima]]
・bash の =~~
授業で使えるかな,と fedora core5 の /etc/rc.d/rc.sysinit を読んでたら,
# Print a text banner.
echo -en $"\t\tWelcome to "
read -r redhat_release < /etc/redhat-release
if [[ "$redhat_release" =~ "Red Hat" ]]; then
[ "$BOOTUP" = "color" ] && echo -en "\\033[0;31m"
echo -en "Red Hat"
なんて表現を発見.=~ は文字列のパターンマッチみたいなんだけど,「bash
にこんな機能あったっけ?」と bash のマニュアルをあれこれ眺めてみたけど
それらしい記述は見当たらず.
よく分からんなぁ,,と,手元の /usr/share/doc/bash/NEWS.gz を見ると,
"New Features in Bash" として
cc. The [[ ... ]] command has a new binary `=~' operator that performs
extended regular expression (egrep-like) matching.
dd. `configure' has a new `--enable-cond-regexp' option (enabled by default)
to enable the =~ operator and regexp matching in [[ ... ]].
ee. Subexpressions matched by the =~ operator are placed in the new
BASH_REMATCH array variable.
なんてのが上げられていて,bash-3.0 の新機能なのか,ということで納得.
確かに条件式の中で文字列のパターンマッチが書けるのはすごく魅力的なんだ
けど,bash-3.0 以上限定になるというのはちょっと悩ましいところだなぁ.
#comment