3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

stdout/stderr のリダイレクトとパイプの整理

Posted at

ほぼこちらを自分用に整理したものです。ストック/お気に入りは元記事へどうぞ
bash: 標準出力、標準エラー出力をファイル、画面それぞれに出力する方法 - Qiita

print.sh
# stdout へ Standard Out , stderr へ Standard Error という文字列を出力する
echo "Standard Out" >&1
echo "Standard Error" >&2

リダイレクト先を stdout/stderr のどっちにするか

stdout へリダイレクト

print.sh >&1

stderr へリダイレクト

print.sh >&2

リダイレクト元を stdout/stderr どっちから取るか

stdout からリダイレクト

print.sh > log
(print.sh 1> log も同じ)

stderr からリダイレクト

print.sh 2> log

stdout/stderr ともにリダイレクト

print.sh &> log

stdout, stderr をそれぞれ別個にリダイレクト

print.sh 1> stdout.log 2> stderr.log

パイプ

stdout をパイプ

print.sh | cat > log

stderr をパイプ

やる方法はあるけどめんどくさいので略

stdout, stderr 両方をパイプ

print.sh 2>&1 | cat > log (まず 2>&1 で stderr を stdout にリダイレクトしてからパイプにつなぐ)
(print.sh |& cat > log も同じ。&| ではない ことに注意)

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?