

如何在Bash中将stderr重定向到stdout
source link: https://www.myfreax.com/bash-redirect-stderr-stdout/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

将命令的输出重定向到文件或将其通过管道传递到另一个命令时,您可能会注意到错误消息已打印在屏幕上。
在Bash和其他Linux Shell中,执行程序时,它使用三个标准I/O流。 每个流由一个数字文件描述符表示:
0
-stdin
,标准输入流。1
-stdout
,标准输出流。2
-stderr
,标准错误流。
文件描述符只是代表打开文件的数字。
输入流通常通过在键盘上输入来向程序提供信息。
程序输出进入标准输出流,错误消息进入标准错误流。 默认情况下,输入流和错误流都打印在屏幕上。
重定向标准输出流
重定向是一种从程序捕获输出并将其作为输入发送到另一个程序或文件的方法。
流可以使用n>
运算符重定向,其中n
是文件描述符。
省略n
时,默认为标准输出流1
。 例如,以下两个命令是相同的; 两者都会将命令输出(stdout
)重定向到文件。
command > file
command 1> file
要重定向标准错误流(stderr
),请使用2>
运算符:
command 2> file
您可以将stderr
和stdout
都写到两个单独的文件中:
command 2> error.txt 1> output.txt
要禁止在屏幕上显示错误消息,请将stderr
重定向到/dev/null
:
command 2> /dev/null
将stderr
重定向到stdout
将程序的输出保存到文件中时,通常会将stderr
重定向到stdout
,以便将所有内容都保存在一个文件中。
要将stderr
重定向到stdout
并将错误消息发送到与标准输出相同的文件,请使用以下命令:
command > file 2>&1
> file
将stdout
重定向到file
,2>&1
将stderr
重定向到stdout
的当前位置。
重定向的顺序很重要。 例如,以下示例仅将stdout
重定向到file
。 以下这种情况是因为stderr
重定向到stdout
,然后stdout
重定向到了file
。
command 2>&1 > file
将stderr
重定向到stdout
的另一种方法是使用&>
构造。 在Bash中,&>
与2>&1
的含义相同:
command &> file
在命令行上工作时,了解重定向和文件描述符的概念非常重要。
要重定向stderr
和stdout
,请使用2>&1
或&>
构造。
如果您有任何问题或反馈,请随时发表评论。
Recommend
-
70
-
19
1 简介 Nginx 是一个非常强大和流行的高性能Web服务器。本文讲解 Nginx 如何整合 https 并将 http 重定向到 https 。 https 相关...
-
30
Introduction Among the popular operating systems, they have all standardized on using standard input, standard output, and standard error with file desciptors 0, 1, and 2 respectively. This allows you to pipe the inputs and outputs...
-
20
Introduction When you use print() in python the output goes to standard output or sys.stdout. You can directly call sys.stdout.write() instead of using print(), but you can also co...
-
28
Introduction Operating systems recognize a couple special file descriptor IDs: STDIN - 0 - Input usally coming in from keyboard. STDOUT - 1 - Output from the...
-
17
Bash打印中的stdout与stderr 2017-07-26 10:58:36 +08 字数:2134 标签: Bash 简介stdout与stderr
-
12
命令列工具的 stdout, stderr 輸出與 .NET 整合應用-黑暗執行緒 昨天提到的 Linux 掃描工具 - scanimage,剛好有個經典輸出分流行為,scanimage 將圖檔傳到標準輸出(Standard Output),...
-
6
Nginx404错误设置301重定向到其它页面的办法 2021-12-3111:59:381 543字 众所周知,404 错误直接影响...
-
3
假如有这样一个场景:我们运行了一个 Linux 命令,它有很多的输出信息,但是我们并不需要这些输出信息。
-
6
访问 bing.com/new 总是被重定向到 https://cn.bing.com/ 看到很多人在说访问国际版 bing 问题总是被跳转问题是因为设置地区,因为我今天也遇到了,所以做个复盘,我只使用前置代理的时候访问国际版是不会跳转的,因为 dns 全局...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK