Linux 標準出力 /dev/null 2>&1の使い方
環境
RHEL8.6
概要
ファイルディスクリプタ
Unixには、次の3つの入出力があり、それぞれ番号が振られています。
0: 標準入力
1: 標準出力
2: 標準エラー出力
使用例
1.test.shの内容
#/bin/bash
echo "12345"
#/bin/bash
echo "12345"
#/bin/bash echo "12345"
ファイルにリダイレクトする場合、シェルを実行します
$ ./test.sh > result.txt
$ cat result.txt
12345
2.特定のファイルディスクリプタにリダイレクトさせます
実行結果
$ ./test.sh >&2
12345
$ ./test.sh >&2
12345
$ ./test.sh >&2 12345
3.逆に標準出力にリダイレクトさせます
$ ./test.sh >&1
12345
$ ./test.sh >&1
12345
$ ./test.sh >&1 12345
4. 出力結果を捨てます
/dev/nullというのはunixのスペシャルファイルです、空ファイルです。
$ ./test.sh > /dev/null
$
$ ./test.sh > /dev/null
$
$ ./test.sh > /dev/null $