shell输入输出重定向

标准输入:0  < , <<或者0< 0<<

标准输出:1  >,>> 或者1>,1>>

错误输出:2  2>>,2>

/dev/null 这个设备,是linux 中黑洞设备,什么信息只要输出给这个设备,都会给吃掉
将标准输出,错误输出都发送给/dev/null中
$ ls test.sh test1.sh >/dev/null 2>&1
 
&,&1代表标准输出
&2代表错误输出

//错误输出直接被写在标准输出中,即在终端上屏幕显示。

:~$ ls -yz 2>&1
ls: invalid option -- 'y'
Try 'ls --help' for more information.

//错误输出会被输出到标准输出,即屏幕显示,而不会写在fname文件中。
:~$ ls -yz 2>&1 1>fname
ls: invalid option -- 'y'
Try 'ls --help' for more information.
:~$ ls
AndroidStudioProjects  Desktop    Documents  Downloads  examples.desktop    fname  Music  Pictures    Public    Templates  usr    Videos    work
:~$ cat fname

//标准输出会写在fname文件中,错误输出会被输出到标准输出中,即文件fname中。
:~$ ls -yz 1>fname 2>&1
:~$ ls
AndroidStudioProjects  Desktop    Documents  Downloads  examples.desktop    fname  Music  Pictures    Public    Templates  usr    Videos    work
:~$ cat fname
ls: invalid option -- 'y'
Try 'ls --help' for more information.

原文地址:https://www.cnblogs.com/shamoguzhou/p/6893504.html