我正在学习 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 作为标准输入。并且最终输出将是空的,因为错误消息中的任何单词都不匹配。但是我收到了错误消息。
上面的代码发生了什么?恐怕我做了一个廉价的误会。请教我。
最佳答案
|
只连接标准输出,但cat
将错误消息(如预期)打印到标准错误。
正如手册页所说,使用 |&
还将标准错误连接到 grep
的标准输入:
cat non_exist_file |& grep -e 'dummy'
关于bash - 为什么命令错误| grep 'regex-not-matching-error' 仍然打印错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66106967/