Bash 重定向

Bash redirections

有人可以解释一下 bash 的以下行为吗?如果我的理解是正确的:

  1. echo abcd > abc def

    the echo abcd prints it out to std out stream, but since due to the presence of ">" it is redirected to the file abc How is def stored as a string in the file abc and not as another file containing the string abcd?

  2. echo abcd > abc > def

    This results in the string abcd to be stored in the file def and the file abc remains empty. How ?

谢谢。

在这个命令中:

echo abcd > abc def foo bar

只有 > 之后的参数用于输出文件名,其余参数用于 echo。因此你得到:

cat abc
abcd def foo bar

然后在这个命令中:

echo abcd > abc > def > xyz

只有>之后的最后一个文件名才会真正输出内容,其余的将为空:

cat xyz
abcd
cat def
cat abc

要将输出存储在多个输出文件中,请像这样使用 tee(抑制标准输出):

date | tee abc def xyz > /dev/null

然后检查内容:

cat abc
Mon Dec  7 07:34:01 EST 2015
cat def
Mon Dec  7 07:34:01 EST 2015
cat xyz
Mon Dec  7 07:34:01 EST 2015