一時ファイルを作成せずに、コマンドの実行結果のdiffを取る (bashのProcess Substitutionで)

コマンドの実行結果を名前付きパイプで読めるらしい。

Command list enclosed within parentheses
>(command_list)
<(command_list)
Process substitution uses /dev/fd/ files to send the results of the process(es) within parentheses to another process.

man bash にも書いてあった。


探し求めてた機能の1つだ。これ知らなかったから今までは、

$ ./hoge > hoge.out
$ ./fuga > fuga.out
$ diff -u hoge.out fuga.out

こんな風にtmpファイル作成してdiffってた。

Process Substitution

$ diff <(./hoge) <(./fuga)
$ diff <(date) <(sleep 1; date)
1c1
< Fri May 29 23:17:31 JST 2009
---
> Fri May 29 23:17:32 JST 2009

unified形式にすると、ファイルディスクリプタが割り当てられていることも確認できる。

$ diff -u <(date) <(sleep 1; date)
--- /dev/fd/63	2009-05-31 19:31:59.000000000 +0900
+++ /dev/fd/62	2009-05-31 19:31:59.000000000 +0900
@@ -1 +1 @@
-Sun May 31 19:31:59 JST 2009
+Sun May 31 19:32:00 JST 2009

わざわざ一時ファイルを作るまでではないけど、diffで確認したいときとかに使える?他には、パイプで出力を渡したいけど、入力は引数のファイルからのみで、標準入力を受け付けてくれないコマンド(例が思いつかない。そんなのがあるのかわからないけど)のために使うとか。

ちなみに">(command_list)" のほうの有効な使い方は良くわからない。