こんにちは。
標準エラー出力のリダイレクト 2>&1
を調べました1 2。
- 『標準エラー出力の割当先を、「規定の標準出力割当て先」へ合流とする』ことになります。
動作例
$ function test_output() { perl -e 'print "1\n"; warn "2\n2\n";'; }; test_output
1
2
2
$ test_output 2>&1 | wc -l
3
$ test_output 1>/dev/null 2>&1 | wc -l
0
$ test_output 2>&1 1>/dev/null | wc -l
2
$ test_output 1>temp.txt 2>&1; cat temp.txt
2
2
1
文法説明
file descriptor (FD)
-
1
:標準出力 -
2
:標準エラー出力
出力割当て先
- 画面:デフォルト
- ファイル:書込み
-
/dev/null
:捨てる
出力リダイレクト(規定の出力割当て先を変更)
-
>
:上書き型 -
>>
:追加書き型
出力リダイレクト&合流割当て
-
>&
FD-
>&1
:標準出力割当先へ
-
合流とはならない例
$ test_output 1>temp.txt 2>temp.txt; cat temp.txt
$ test_output 2>temp.txt 1>temp.txt; cat temp.txt
1
2
$