linux系统shell中重定向 标准输出 错误输出 >/dev/null 2>&1

经常shell脚本中,会看到: 仔细体会下面例子

>/dev/null  2>&1 这句话的意思简单理解为:标准输出和错误输出都不会输出出来。

root@tcas303:/tmp>date>1.txt >/dev/null 2>&1
root@tcas303:/tmp>cat 1.txt 
root@tcas303:/tmp>

当然也可以拆开了用:

>/dev/null 意思为:标准输出不输出

root@tcas303:/tmp>date >/dev/null
root@tcas303:/tmp>
或者
root@tcas303:/tmp>date>1.txt >/dev/null
root@tcas303:/tmp>cat 1.txt 
root@tcas303:/tmp>

2>&1 意思为:标准输出和出错输出同时输出

root@tcas303:/tmp>data>1.txt 2>&1
root@tcas303:/tmp>cat 1.txt
-bash: data: command not found

 2>&1 >/dev/null 意思为正确输出不输出,只输出错误输出

root@tcas303:/tmp>data>1.txt 2>&1 >/dev/null
root@tcas303:/tmp>cat 1.txt 
-bash: data: command not found

参考:shell中>/dev/null 2>&1

Shell标准输出、标准错误 >/dev/null 2>&1

猜你喜欢

转载自blog.51cto.com/weiruoyu/2313059