LoginSignup
82
84

More than 5 years have passed since last update.

Bashの入出力リダイレクトまとめ

Posted at

チートシート

# 入力
command < file   # ファイルの内容をコマンドの標準入力に渡す

#-----------------------------------------------------------
# 出力
command >&2      # 標準出力を標準エラー出力にリダイレクト

command > file   # ファイル作成 or 上書き
command >> file  # 追加出力。ファイルがなければ作成
command 2> file  # 標準エラー出力をファイルにリダイレクト(作成 or 上書き)

command &> file      # 標準出力/エラー出力を同一ファイルにリダイレクト
command > file 2>&1  # 同上
command &>> file     # 標準出力/エラー出力を同一ファイルに追加書き込み
command >> file 2>&1 # 同上

command > file1 2> file2   # 標準出力,エラー出力を別々のファイルにリダイレクト
command >> file1 2>> file2 # 標準出力,エラー出力を別々のファイルに追加書き込み

NOTE:

  • &>>& に置き換え可能だが、Man page of BASHによれば好ましいのは前者らしい。
  • 同様に、&>>>>& に置き換え可能。
  • &>, &>> 辺りはBash以外のシェルでは動かないことがあったと思う。

参考

82
84
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
82
84