LoginSignup
5
3

More than 5 years have passed since last update.

/dev/null に関するメモ

Last updated at Posted at 2018-10-02

/dev/nullに関してメモっとく。

/dev/null って結局なんなん?

/dev/nullは空ファイルって意味なんやて。要するにゴミ箱って認識でいいと思うの。
結果を出力したくないとか捨てたいときに使う。

出力結果を捨てる

command > /dev/nullみたいに出力結果を捨てるときに/dev/nullを使う。>はリダイレクト。

例えば、if文とかで出力結果は捨てたい時とかに使う。

PC0339% if which cd > /dev/null; then echo "hoge"; fi 
hoge
PC0339% if which cd; then echo "hoge"; fi
cd: shell built-in command
hoge
PC0339% 

1回目のコマンドはif文の結果のhogeしか出力されていないが、2回目のコマンドはwhichの結果も出力されている。

command > /dev/null 2>&1について

よくcrontabで見かける記述だが、これは標準出力とエラー出力をどっちも捨てるという意味。コマンドをそれぞれ分解すると、

  • : 標準出力
  • 2 : 標準エラー出力
  • >& : マージ

つまり、2>&1は標準エラー出力を標準出力にマージという意味。
よって、上記のコマンドの意味は、標準エラー出力を標準出力にマージしてから捨てるという意味になる。まどろっこしいぜ。

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