Bash I/O Redirection Summary

64

Cheat sheet

# 入力
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:

  • &> can be replaced by >&, but according to the Man page of BASH, the former is preferred.
  • Similarly, &>> can be replaced by >>&.
  • &>, &>> I think there were times when it didn't work with shells other than Bash.

reference

Share:
64
progrhyme
Author by

progrhyme

Software Engineer. Was @key-amb

Updated on August 25, 2018