LoginSignup
0
0

More than 1 year has passed since last update.

標準エラー出力を標準出力へリダイレクト(&合流)

Last updated at Posted at 2023-04-29

こんにちは。
標準エラー出力のリダイレクト 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
$
  1. 参考:「>/dev/null 2>&1は順序が逆だとダメ

  2. 参考:「いい加減覚えよう。 command > /dev/null 2>&1の意味

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0