LoginSignup
76
64

More than 5 years have passed since last update.

リダイレクションの「>」「 >>」とパイプ「 | 」の違い。

Last updated at Posted at 2016-05-01

概要


Unix、Linuxのコマンドで、標準出力の結果を何かに渡したい時に、リダイレクションである「>」「>>」、パイプの「 | 」の違いを説明します。

リダイレクション


リダイレクションは、標準出力をテキストファイルに渡したいときに使用します。

「>」「>>」の違い

項目 説明
> ファイルへの上書き
>> ファイルへの追記

サンプル

sample.list
$ cat sample.list 
1 Ichiro ichiro@sample.com
2 Jiro   jiropsample.com

「 >> (追記)」の場合

#変数varに"3 tarou tarou@sample.com"の情報を設定
$ var="3 tarou tarou@sample.com"
$ echo ${var}
3 tarou tarou@sample.com

# >>で追記します。
$ echo $var >> sample.list 
$ cat sample.list 
1 Ichiro ichiro@sample.com
2 Jiro   jiropsample.com
3 tarou tarou@sample.com

「 > 」の場合

# sample.listの中身
$ cat sample.list
1 Ichiro ichiro@sample.com
2 Jiro   jiropsample.com

#変数varに"reset"を設定
$ var="reset"
$ echo ${var}
reset

# 結果
$ echo ${var} > sample.list
$ cat sample.list
reset

パイプ


パイプは、標準出力をコマンドに渡したいときに使用します。

サンプル

# sample.listの中身
$ cat sample.list 
1 Ichiro ichiro@sample.com
2 Jiro   jiropsample.com
3 tarou tarou@sample.com

# Q sample.listのIchiroさんのアドレスを知りたい。
# A grepコマンドとawkコマンドをパイプを使って、抽出しています。
$ grep 'Ichiro' sample.list | awk '{print $3}'
ichiro@sample.com 
76
64
2

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
76
64