【发布时间】:2019-05-15 00:14:04
【问题描述】:
据我了解,2>&1 仅表示“将 stderr 发送到与 stdout 相同的位置”。但由于某种原因,1>foo 2>foo 和 1>foo 2>&1 似乎并不相同。
# Assuming file 'out' exists but file 'nosuchfile' doesn't
# 'foo' contains stdout and some partial stderr in CentOS 6
$ ls -l out nosuchfile 1>foo 2>foo
$ cat foo
-rw-r--r-- 1 user user 0 May 14 14:45 out
ctory
# 'foo' contains both stdout and stderr
$ ls -l out nosuchfile 1>foo 2>&1
$ cat foo
ls: cannot access nosuchfile: No such file or directory
-rw-r--r-- 1 user user 0 May 14 14:45 out
谁能解释为什么他们的行为不同?
【问题讨论】:
-
这可能是因为 stderr 没有完全缓冲(你的第一种情况)。在第二个例子中,一切都进入标准输出,标准输出进入文件 foo;重定向到文件时,stdout 完全缓冲。见pubs.opengroup.org/onlinepubs/9699919799/functions/…。