经常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