LoginSignup
0
1

More than 5 years have passed since last update.

エラー出力を標準出力にまとめる

Posted at
  • 2>&1を使うとエラー出力を標準出力にまとめる事ができます
$ sh -c "date" > ./log # dateの標準出力がshの標準出力としてlogに書き出される
$ cat log
2018年  9月  7日 金曜日 16:36:08 JST

$ sh -c "dat" > ./log # dat(dateのtypo)を実行するとエラーが出る、エラーはshの標準出力に含まれないため logには空文字が書き込まれる
$ cat log # 当然何も出力されない

$ sh -c "date 2>&1" > ./log # dateのエラーも標準出力にまとめる、logにはdateの出力全てが書き出される
$ cat log
2018年  9月  7日 金曜日 16:41:38 JST

$ sh -c "dat 2>&1" > ./log # datのエラーも標準出力にまとめる、logにはdatの出力全てが書き出される
$ cat log
sh: 1: dat: not found # これはlogの中身
0
1
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
1