【发布时间】:2021-05-12 08:59:00
【问题描述】:
我正在学习 bash。 我运行了以下代码并得到了一些输出,
$ cat non_exist_file | grep -e 'dummy'
cat: non_exist_file: No such file or directory
这对我来说很奇怪,因为我预计输出应该什么都没有。
我已经阅读了下面 bash 手册中的说明,
Pipelines
[time [-p]] [ ! ] command [ [|⎪|&] command2 ... ]
...
If |& is used, command's standard error, in addition to its standard output,
is connected to command2's standard input through the pipe; it is shorthand
for 2>&1 |.
根据上面的指令,我预计管道会传递错误消息,
cat: non_exist_file: No such file or directory
到 grep 作为标准输入。最终输出将是空的,因为错误消息中的任何单词都不匹配。但是我收到了错误消息。
上面的代码发生了什么?恐怕我做了一个廉价的误会。请教我。
【问题讨论】:
标签: bash