请教下 Linux 的输入输出

2021-03-21 00:26:30 +08:00
 mingtdlb

&> file 、>& file 、>file 2>&1,这三个有啥区别?

1924 次点击
所在节点    Linux
4 条回复
yinusxxxx
2021-03-21 00:43:55 +08:00
Redirecting Standard Output and Standard Error
This construct allows both the standard output (file descriptor 1) and
the standard error output (file descriptor 2) to be redirected to the
file whose name is the expansion of word.

There are two formats for redirecting standard output and standard
error:

&>word
and
>&word

Of the two forms, the first is preferred. This is semantically equiva-
lent to

>word 2>&1

When using the second form, word may not expand to a number or -. If
it does, other redirection operators apply (see Duplicating File
Descriptors below) for compatibility reasons.


来自 man bash,综上所述,这三个没有功能上的区别,写法不同而已
Jat001
2021-03-21 00:44:37 +08:00
&> file 跟 >file 2>&1 一样,都是把标准输出 stdout 和错误输出 stderr 写到 file 里,1 代表标准输出,2 代表错误输出
2>&1 的意思把 stderr 写入到 stdout 里,>file 在前还是再后无所谓,最后都是写到文件里

>& file 还真没见过
shakoon
2021-03-21 08:34:43 +08:00
1 的错误输出在 console,不会写入 file,其他输出写入 file
2 没见过这种写法,不知道
3 错误输出到 file,console 什么也不会输出,适合完整记录日志
楼上两位说的不对,至少 1 和 3 是有明显区别的
no1xsyzy
2021-03-21 23:48:53 +08:00
@shakoon
(echo out; echo err >&2 ) &> temp1.txt
(echo out; echo err >&2 ) >& temp2.txt
(echo out; echo err >&2 ) > temp3.txt 2>&1
看看结果?

@mingtdlb 这不是 Linux 的任何方面,这是 Bash 语法

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/763553

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX