備忘録
解決策:
(time ${some_command} 1>/dev/null 2>&1) 2>&1
例
次のような標準エラーにhello、標準出力にworldを出力するtest.shがあるとき、
test.sh
#!/bin/bash
echo hello 1>&2
sleep 1
echo world
上記のコマンドラインを実行する。結果をteeコマンドで受けファイルに書き込む。
(time ./test.sh 1>/dev/null 2>&1) 2>&1 | tee result.txt
結果はつぎのようになる。
result.txt
real 0m1.423s
user 0m0.002s
sys 0m0.008s
./test.shの標準出力をNullデバイスにリダイレクトし、エラーを標準出力(つまりNullデバイス)にリダイレクトする。そしてtimeコマンドのエラー出力を標準出力にリダイレクトする。(これはNullデバイスを指さない。かっこの位置に注目。)
↧