擅长:python、mysql、java
<p>由于程序可以确定其输出FD是否指向tty,因此重定向可能什么也不做。</p>
<p>很可能stdout在指向终端时是行缓冲的(与C的<a href="http://linux.die.net/man/3/stdout" rel="noreferrer">^{<cd1>}</a>流行为相同)。</p>
<p>作为一个有趣的实验,尝试将输出设置为<code>cat</code>。</p>
<hr/>
<p>我试过自己有趣的实验,结果如下。</p>
<pre><code>$ python test.py 2>foo
...
$ cat foo
-----
timing summary (100k lines each)
-----
print : 6.040 s
write to file : 0.122 s
print with stdout = /dev/null : 0.121 s
$ python test.py 2>foo |cat
...
$ cat foo
-----
timing summary (100k lines each)
-----
print : 1.024 s
write to file : 0.131 s
print with stdout = /dev/null : 0.122 s
</code></pre>