bash >& 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11255447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does >& mean?
提问by contrapositive
I was a little confused by this expression:
我对这个表达有点困惑:
gcc -c -g program.c >& compiler.txt
I know &>filename
will redirect both stdout and stderr to file filename
. But in this case the ampersand is after the greater than sign. It looks like its of the form M>&N
, where M
and N
are file descriptors.
我知道&>filename
会将 stdout 和 stderr 都重定向到 file filename
。但在这种情况下,&符号在大于号之后。它看起来像它的形式M>&N
,其中M
和N
是文件描述符。
In the snippet above, does M=1
and N='compiler.txt'
? How exactly is this different from:
在上面的代码片段中,doesM=1
和N='compiler.txt'
? 这与以下具体有何不同:
gcc -c -g program.c > compiler.txt (ampersand removed)
My understanding is that each open file is associated with a file descriptor greater than 2. Is this correct?
我的理解是每个打开的文件都与一个大于 2 的文件描述符相关联。这是正确的吗?
If so, is a file name interchangeable with its file descriptor as the target of redirection?
如果是这样,文件名是否可以与其文件描述符互换作为重定向的目标?
回答by jordanm
This is the same as &>
. From the bash manpage:
这与&>
. 从 bash 联机帮助页:
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
重定向标准输出和标准错误 此结构允许将标准输出(文件描述符 1)和标准错误输出(文件描述符 2)重定向到名称为 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
回答by Tom Hale
&>
vs >&
: the preferred version is &>
(clobber)
&>
vs >&
: 首选版本是&>
(clobber)
Regarding:
关于:
&>
>&
&>
>&
both will clobber the file - truncate it file to 0 bytes before writing to it, just like > file
would do in the STDIN-only case.
两者都会破坏文件 - 在写入文件之前将其截断为 0 字节,就像> file
在仅使用 STDIN 的情况下一样。
However, the bash
manual Redirections sectionadds that:
但是,bash
手动重定向部分补充说:
Of the two forms, the first is preferred. This is semantically equivalent to
>word 2>&1
When using the second form, wordmay not expand to a number or
-
. If it does, other redirection operators apply (see Duplicating File Descriptors below) for compatibility reasons.
在这两种形式中,首选第一种。这在语义上等同于
>word 2>&1
使用第二种形式时,单词可能不会扩展为数字或
-
。如果是这样,出于兼容性原因,其他重定向运算符将适用(请参阅下面的复制文件描述符)。
(Note: in zsh
both are equivalent.)
(注:在zsh
这两个是等价的。)
It's very good practice to get finger memory in the first (&>
) form, because:
以第一种 ( &>
) 形式获取手指记忆是非常好的做法,因为:
Use &>>
as >>&
is not supprted by bash
(append)
使用&>>
as>>&
不受bash
(append) 支持
There's only one append form:
只有一种附加形式:
The format for appending standard output and standard error is:
&>>word
This is semantically equivalent to
>>word 2>&1
(see Duplicating File Descriptors below).
附加标准输出和标准错误的格式是:
&>>word
这在语义上等同于
>>word 2>&1
(请参阅下面的复制文件描述符)。
Note:
笔记:
- The clobber usage of
&>
over>&
in the section above is again recommended given there is only one way for appending inbash
. zsh
allows both&>>
and>>&
forms.
- 的撞使用
&>
过>&
的部分上方再次推荐给有在附加只有一个办法bash
。 zsh
允许&>>
和>>&
形式。