一般情况下,每个 Unix/Linux 命令运行时都会打开三个文件:
默认情况下:
command > file将 stdout 重定向到 filecommand 2 > file将 stderr 重定向到 filecommand < file将stdin 重定向到 file。command > file 2>&1将 stderr 和 stdout合并后重定向到 filecommand > file1 < file2将 stdout 重定向到 file1,stdin 重定向file2command 2 >> file将 stderr 追加到 file末尾
如果希望执行某个命令,但又不希望在屏幕上显示输出结果,那么可以将输出重定向到 /dev/null
command > /dev/null 2>&1屏蔽 stdout 和 stderr
#! /bin/bash if command -v git >/dev/null 2>&1; then echo 'exists git' else echo 'no exists git' fi
#! /bin/bash if type git >/dev/null 2>&1; then echo 'exists git' else echo 'no exists git' fi
#! /bin/bash if hash git 2>/dev/null; then echo 'exists git' else echo 'no exists git' fi
https://www.jianshu.com/p/fbffa5cc49e1 http://c.biancheng.net/cpp/view/2738.html
一般情况下,每个 Unix/Linux 命令运行时都会打开三个文件:
默认情况下:
command > file将 stdout 重定向到 filecommand 2 > file将 stderr 重定向到 filecommand < file将stdin 重定向到 file。command > file 2>&1将 stderr 和 stdout合并后重定向到 filecommand > file1 < file2将 stdout 重定向到 file1,stdin 重定向file2command 2 >> file将 stderr 追加到 file末尾
如果希望执行某个命令,但又不希望在屏幕上显示输出结果,那么可以将输出重定向到 /dev/null
command > /dev/null 2>&1屏蔽 stdout 和 stderr
#! /bin/bash if command -v git >/dev/null 2>&1; then echo 'exists git' else echo 'no exists git' fi
#! /bin/bash if type git >/dev/null 2>&1; then echo 'exists git' else echo 'no exists git' fi
#! /bin/bash if hash git 2>/dev/null; then echo 'exists git' else echo 'no exists git' fi
https://www.jianshu.com/p/fbffa5cc49e1 http://c.biancheng.net/cpp/view/2738.html