重定向"1"/dev/null 2>和"1"之间的区别是什么?和"2& 1 1>/dev/null"? [英] Whats the difference between redirections "1>/dev/null 2>&1" and " 2>&1 1>/dev/null"?

查看:94
本文介绍了重定向"1"/dev/null 2>和"1"之间的区别是什么?和"2& 1 1>/dev/null"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重定向之间有什么区别1>/dev/null 2>& 1和2>& 1 1>/dev/null

Whats the difference between redirections 1>/dev/null 2>&1 and 2>&1 1>/dev/null

似乎第一个显示输出到stdout,而不显示第二个.

It seems 1st one displays output to stdout but not the second one.

有人可以解释!谢谢

推荐答案

扩大Lokendra26的答案:/dev/null是系统上的一个特殊文件,该设备用于丢弃写入其中的所有内容.如果您不想看到输出,通常将其发送到那里.在这种情况下,文件"(通常是unix术语)既可以是普通磁盘文件,也可以是诸如null设备或终端之类的设备.

Expanding on Lokendra26's answer a bit: /dev/null is a special file on your system, a device for discarding anything written to it. It's common to send output there if you don't want to see it. "File" in this case, and unix terminology in general, can be both a normal disk file, or a device like the null device or your terminal.

"1"和"2"是文件描述符,是用于发送输出的位置的指示符.程序使用FD 1(标准输出)作为普通输出的目标,而使用FD 2(标准错误)作为错误输出的目标.这些文件描述符可以在不同的时间指向不同的文件.通常它们都指向您的终端,因此您可以从那里编写的程序中获取输出.

The "1" and "2" are file descriptors, designators for places to send output. Programs use FD 1, "standard output", as the target for ordinary output, and FD 2, "standard error", for error output. These file descriptors can point to different files at different times. Normally they both point at your terminal, so you se output from your programs written there.

&运算符不只是为了消除歧义.它实际上意味着在此点上查找此FD指向的任何内容".

The & operator is more than just for disambiguation. It actually means "look up whatever this FD points to at this point".

理解这些详细信息以了解您要询问的两个重定向之间的区别很重要.

It is important to understand these details in order to understand the difference between the two redirections you are asking about.

1>/dev/null 2>& 1 实际上是两个按顺序处理的语句.首先,将标准输出"指向空设备(从而丢弃写入其中的所有内容).其次,将标准错误"指向标准输出"所指向的任何位置,在本例中为/dev/null.最终结果是两个文件描述符的输出都将被丢弃.

1>/dev/null 2>&1 this is actually two statements, processed in sequence. First, point "standard output" at the null device (thus discarding anything written to it). Second, point "standard error" at whatever "standard output" is pointing to, in this case /dev/null. The end result is that output from both file descriptors will be discarded.

2>& 1 1>/dev/null 同样是两个语句.首先,将标准错误"指向标准输出"所指向的位置.就像我上面写的那样,通常这将是您的终端.其次,将标准输出"指向/dev/null.最终结果-仅丢弃标准输出",标准错误"仍将打印到您的终端.

2>&1 1>/dev/null is likewise two statements. First, point "standard error" at whatever "standard output" is pointing to. Normally this will be your terminal, as I wrote above. Second, point "standard output" at /dev/null. End result - only "standard output" is discarded, "standard error" will still print to your terminal.

这篇关于重定向"1"/dev/null 2>和"1"之间的区别是什么?和"2& 1 1>/dev/null"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆