2012.04.20
■[bash] mv !(dir) dir/ の !(dir) について調べた記録
きっかけ
- カレント・ディレクトリー内の全てのファイルを新しい下位ディレクトリーに簡単に移動させる 〜 zsh 編 - clmemo@aka
- bashとzshで使える、特定ファイルを除外するglob記法 - Humanity
mv !(dir) dir/ で、bash でも カレント・ディレクトリー内の全てのファイルを新しい下位ディレクトリーに簡単に移動させられるとのこと。「これだと dir という名前以外のディレクトリがあった場合うまくゆかいない」というツッコミはしないことにします。それより bash の !(dir) という書き方を見たことがなかったので調べると、extglob が有効なら使えるパターンマッチで、カッコ内のパターン以外にマッチする表現でした。extglob は Ubuntu 11.10 の bash 4.2.10 ではデフォルトで有効になっていました。
$ shopt | grep extglob extglob on
▼ Bash Reference Manual
3.5.8.1 Pattern Matching
...
If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized. In the following description, a pattern-list is a list of one or more patterns separated by a ‘|’. Composite patterns may be formed using one or more of the following sub-patterns:
?(pattern-list)
Matches zero or one occurrence of the given patterns.
*(pattern-list)
Matches zero or more occurrences of the given patterns.
+(pattern-list)
Matches one or more occurrences of the given patterns.
@(pattern-list)
Matches one of the given patterns.
!(pattern-list)
Matches anything except one of the given patterns.
CHANGES によると、この機能は ksh 由来で bash-2.02-alpha1 から使えたようです。
This document details the changes between this version, bash-2.02-alpha1,
and the previous version, bash-2.01.1-release.
1. Changes to Bash
...
2. Changes to Readline
...
d. ksh-88 egrep-style extended pattern matching ([@+*?!](patlist)) has been
implemented, controlled by a new `shopt' option, `extglob'.
