理系学生日記

おまえはいつまで学生気分なのか

標準出力と標準エラー出力を同時にパイプに送る時のTips

標準エラー出力と標準出力を同時にパイプに送るとき、2>&1 | と記述することが多いでしょう。

$ ls /etc/hosts /notexist 2>&1 | cat > ls.dat
$ cat -n ls.dat
     1  ls: cannot access '/notexist': No such file or directory
     2  /etc/hosts

Bash Reference Manualを読んでいると、この2>&1 ||&という短縮記法で書けるということに気付きました。

$ ls /etc/hosts /notexist |& cat > ls2.dat
$ cat -n ls2.dat
     1  ls: cannot access '/notexist': No such file or directory
     2  /etc/hosts

(読みにくいので)あまり使うことはなさそうですが…。