将 stderr 定向到 stdout $ command-name 2>&1 默认情况下,stderr 不会打印到终端吗?
每当我在 bash shell 中遇到错误(例如语法很差)时,错误消息总是会打印到终端。
$ totem Desktop/songs/song1.mp3
** Message: Error: Resource not found.
gstfilesrc.c(1055): gst_file_src_start (): /GstPlayBin2:play/GstURIDecodeBin:uridecodebin0/GstFileSrc:source:
No such file "/home/me/Desktop/songs/song1.mp3"
那么,如果 stderr 默认打印到终端,那么将 stderr 重定向到 stdout 的目的是什么?
$ command-name 2>&1
The error message always gets printed to the terminal whenever I get an error in bash shell (e.g. poor syntax)
$ totem Desktop/songs/song1.mp3
** Message: Error: Resource not found.
gstfilesrc.c(1055): gst_file_src_start (): /GstPlayBin2:play/GstURIDecodeBin:uridecodebin0/GstFileSrc:source:
No such file "/home/me/Desktop/songs/song1.mp3"
So what is the purpose of redirecting stderr to stdout if stderr gets printed to terminal by default?
$ command-name 2>&1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,
stdout
和stderr
都定向到终端。如果您将stderr
重定向到stdout
,那么它也会转到终端。如果你想摆脱 stderr 输出,请将其重定向到 void:
By default, both
stdout
andstderr
are directed to the terminal. If you redirectstderr
tostdout
, then it also goes to the terminal.If you want to get rid of the stderr output, redirect it to the void:
请注意,默认情况下,
stdout
会打印到终端。通过执行command-name 2>&1
,您实际上将stderr
重定向到同一位置。重定向的顺序也很重要。
所以你要找的是,
command-name >file_for_stdout 2>&1
Note that
stdout
gets printed to the terminal by default. and by doingcommand-name 2>&1
you are essentially redirectingstderr
to the same place.Also order of redirection matters.
So what you are looking for is ,
command-name >file_for_stdout 2>&1
stderr
和stdout
默认情况下都显示在控制终端上。如果像之前那样对它们进行多路复用,则它们都将输出到任何stdout
输出上。然后,您可以将stdout
重定向到文件以在单个文件中获取输出和错误。会将错误和输出放入名为“log.log”的文件中。
stderr
andstdout
are by default both displayed on the controlling terminal. If you multiplex them as you've done, they will both be output on the whateverstdout
outputs to. You can then redirectstdout
to a file to get output and errors in a single file.will put both errors and output into the file called `log.