In a Unix shell, if I want to combine stderr
and stdout
into the stdout
stream for further manipulation, I can append the following on the end of my command:
在Unix shell中,如果我想将stderr和stdout合并到stdout流中进行进一步的操作,我可以在命令的末尾追加以下内容:
2>&1
So, if I want to use head
on the output from g++
, I can do something like this:
所以,如果我想用g++的输出,我可以这样做:
g++ lots_of_errors 2>&1 | head
so I can see only the first few errors.
所以我只能看到最初的几个错误。
I always have trouble remembering this, and I constantly have to go look it up, and it is mainly because I don't fully understand the syntax of this particular trick.
我总是很难记住这一点,我经常要去查阅它,这主要是因为我没有完全理解这个特殊技巧的语法。
Can someone break this up and explain character by character what 2>&1
means?
有人能打破这个并解释角色的特征2>和1是什么意思?
15 个解决方案
#1
1887
File descriptor 1 is the standard output (stdout
).
File descriptor 2 is the standard error (stderr
).
文件描述符1是标准输出(stdout)。文件描述符2是标准错误(stderr)。
Here is one way to remember this construct (although it is not entirely accurate): at first, 2>1
may look like a good way to redirect stderr
to stdout
. However, it will actually be interpreted as "redirect stderr
to a file named 1
". &
indicates that what follows is a file descriptor and not a filename. So the construct becomes: 2>&1
.
这里有一种方法来记住这个结构(尽管它不是完全正确的):首先,2>1看起来是一个将stderr重定向到stdout的好方法。但是,它实际上会被解释为“将stderr重定向到一个名为1的文件”。表示下面是一个文件描述符而不是文件名。所以构造变成:2>和1。
#2
463
echo test > afile.txt
..redirects stdout to afile.txt
. This is the same as doing..
. .afile.txt重定向标准输出。这跟做一样。
echo test 1> afile.txt
To redirect stderr, you do..
要重定向stderr,你需要。
echo test 2> afile.txt
>&
is the syntax to redirect a stream to another file descriptor - 0 is stdin. 1 is stdout. 2 is stderr.
>&是将流重定向到另一个文件描述符的语法—0是stdin。1是stdout。2是stderr。
You can redirect stdout to stderr by doing..
你可以把stdout重定向到stderr。
echo test 1>&2 # or echo test >&2
..or vice versa:
. .反之亦然:
echo test 2>&1
So, in short.. 2>
redirects stderr to an (unspecified) file, appending &1
redirects stderr to stdout
所以,简而言之. .>将stderr重定向到一个(未指定的)文件,appending &1将stderr重定向到stdout。
#3
249
Some tricks about redirection
Some syntax particularity about this may have important behaviours. There is some little samples about redirections, STDERR
, STDOUT
and arguments ordering.
一些语法的特殊性可能有重要的行为。有一些关于重定向、STDERR、STDOUT和参数排序的小样本。
1 - Overwritting or appending?
Symbole >
mean redirection.
作品喻示>的意思是重定向。
-
>
mean send to as a whole completed file, overwriting target if exist (seenoclobber
bash feature at #3 later). - >的意思是发送到一个完整的文件,如果存在的话覆盖目标(参见第3条的noclobber bash特性)。
-
>>
mean send in addition to would append to target if exist. - >>的意思是,如果存在的话,将附加到目标。
Any case, the file would be created if they not exist.
任何情况下,如果文件不存在,文件就会被创建。
2 - The shell command line is order dependant!!
For testing this, we need a simple command which will send something on both outputs:
为了测试这个,我们需要一个简单的命令,它将在两个输出上发送一些东西:
$ ls -ld /tmp /tnt
ls: cannot access /tnt: No such file or directory
drwxrwxrwt 118 root root 196608 Jan 7 11:49 /tmp
$ ls -ld /tmp /tnt >/dev/null
ls: cannot access /tnt: No such file or directory
$ ls -ld /tmp /tnt 2>/dev/null
drwxrwxrwt 118 root root 196608 Jan 7 11:49 /tmp
(Expecting you don't have a directory named /tnt
, of course ;). Well, we have it!!
(当然,希望您没有一个名为/tnt的目录;)嗯,我们有它! !
So lets see:
所以让我们看看:
$ ls -ld /tmp /tnt >/dev/null
ls: cannot access /tnt: No such file or directory
$ ls -ld /tmp /tnt >/dev/null 2>&1
$ ls -ld /tmp /tnt 2>&1 >/dev/null
ls: cannot access /tnt: No such file or directory
The last command line dump STDERR
to the console, it seem not to be the expected behaviour... But...
最后一个命令行转储STDERR到控制台,它似乎不是预期的行为…但是…
If you want to make some post filtering about one ouput, the other or both:
如果你想要对一个ouput进行过滤,另一个或两者都要:
$ ls -ld /tmp /tnt | sed 's/^.*$/<-- & --->/'
ls: cannot access /tnt: No such file or directory
<-- drwxrwxrwt 118 root root 196608 Jan 7 12:02 /tmp --->
$ ls -ld /tmp /tnt 2>&1 | sed 's/^.*$/<-- & --->/'
<-- ls: cannot access /tnt: No such file or directory --->
<-- drwxrwxrwt 118 root root 196608 Jan 7 12:02 /tmp --->
$ ls -ld /tmp /tnt >/dev/null | sed 's/^.*$/<-- & --->/'
ls: cannot access /tnt: No such file or directory
$ ls -ld /tmp /tnt >/dev/null 2>&1 | sed 's/^.*$/<-- & --->/'
$ ls -ld /tmp /tnt 2>&1 >/dev/null | sed 's/^.*$/<-- & --->/'
<-- ls: cannot access /tnt: No such file or directory --->
Notice that the last command line in this paragraph is exactly same as in previous paraghaph, where I wrote seem not to be the expected behaviour (so, this could even be an expected behaviour).
请注意,这一段中的最后一条命令行与前面的段落中所写的完全相同,我所写的似乎并不是预期的行为(因此,这甚至可能是一种预期的行为)。
Well there is a little tricks about redirections, for doing different operation on both ouputs:
这里有一些关于重定向的小技巧,用于在两个输出上做不同的操作:
$ ( ls -ld /tmp /tnt | sed 's/^/O: /' >&9 ) 9>&2 2>&1 | sed 's/^/E: /'
O: drwxrwxrwt 118 root root 196608 Jan 7 12:13 /tmp
E: ls: cannot access /tnt: No such file or directory
Nota: &9
descriptor would occur spontaneously because of ) 9>&2
.
Nota:和9描述符会自发产生,因为)9>&2。
Addendum: nota! With new version of bash (>4.0
) there is a new feature and more sexy syntax for doing this kind of things:
附录:背板!有了新版本的bash(>4.0),有了一种新的特性和更性感的语法来做这类事情:
$ ls -ld /tmp /tnt 2> >(sed 's/^/E: /') > >(sed 's/^/O: /')
O: drwxrwxrwt 17 root root 28672 Nov 5 23:00 /tmp
E: ls: cannot access /tnt: No such file or directory
And finaly for such a cascading output formatting:
对于这样的级联输出格式,finaly:
$ ((ls -ld /tmp /tnt |sed 's/^/O: /' >&9 ) 2>&1 |sed 's/^/E: /') 9>&1| cat -n
1 O: drwxrwxrwt 118 root root 196608 Jan 7 12:29 /tmp
2 E: ls: cannot access /tnt: No such file or directory
Addendum: nota! Same new syntax, in both ways:
附录:背板!同样的新语法,在两方面:
$ cat -n <(ls -ld /tmp /tnt 2> >(sed 's/^/E: /') > >(sed 's/^/O: /'))
1 O: drwxrwxrwt 17 root root 28672 Nov 5 23:00 /tmp
2 E: ls: cannot access /tnt: No such file or directory
Where STDOUT
go through a specific filter, STDERR
to another and finally both outputs merged go through a third command filter.
STDOUT通过一个特定的过滤器,STDERR到另一个,最后两个输出合并通过第三个命令过滤器。
3 - A word about noclobber
option and >|
syntax
That's about overwritting:
这是关于overwritting:
While set -o noclobber
instruct bash to not overwrite any existing file, the >|
syntax let you pass through this limitation:
当set -o noclobber指示bash不要覆盖任何现有文件时,>|语法允许您通过这个限制:
$ testfile=$(mktemp /tmp/testNoClobberDate-XXXXXX)
$ date > $testfile ; cat $testfile
Mon Jan 7 13:18:15 CET 2013
$ date > $testfile ; cat $testfile
Mon Jan 7 13:18:19 CET 2013
$ date > $testfile ; cat $testfile
Mon Jan 7 13:18:21 CET 2013
File is overwritted each time, well now:
文件每次都被重写,现在:
$ set -o noclobber
$ date > $testfile ; cat $testfile
bash: /tmp/testNoClobberDate-WW1xi9: cannot overwrite existing file
Mon Jan 7 13:18:21 CET 2013
$ date > $testfile ; cat $testfile
bash: /tmp/testNoClobberDate-WW1xi9: cannot overwrite existing file
Mon Jan 7 13:18:21 CET 2013
Pass through with >|
:
通过与> |:
$ date >| $testfile ; cat $testfile
Mon Jan 7 13:18:58 CET 2013
$ date >| $testfile ; cat $testfile
Mon Jan 7 13:19:01 CET 2013
Unsetting this option and/or inquiring if already set.
取消此选项和/或询问是否已设置。
$ set -o | grep noclobber
noclobber on
$ set +o noclobber
$ set -o | grep noclobber
noclobber off
$ date > $testfile ; cat $testfile
Mon Jan 7 13:24:27 CET 2013
$ rm $testfile
4 - Last trick and more...
For redirecting both output from a given command, we see that a right syntax could be:
为了从给定的命令中重定向两个输出,我们可以看到一个正确的语法:
$ ls -ld /tmp /tnt >/dev/null 2>&1
for this special case, there is a shortcut syntax: &>
... or >&
对于这个特殊情况,有一个快捷语法:&>…或> &
$ ls -ld /tmp /tnt &>/dev/null
$ ls -ld /tmp /tnt >&/dev/null
Nota: if 2>&1
exist, 1>&2
is a correct syntaxe too:
如果2个>和1存在,1>和2也是正确的syntaxe:
$ ls -ld /tmp /tnt 2>/dev/null 1>&2
4b- Now, I will let you think about:
$ ls -ld /tmp /tnt 2>&1 1>&2 | sed -e s/^/++/
++/bin/ls: cannot access /tnt: No such file or directory
++drwxrwxrwt 193 root root 196608 Feb 9 11:08 /tmp/
$ ls -ld /tmp /tnt 1>&2 2>&1 | sed -e s/^/++/
/bin/ls: cannot access /tnt: No such file or directory
drwxrwxrwt 193 root root 196608 Feb 9 11:08 /tmp/
4c- If you're interested in more informations
you could Read The Fine Manual by hitting:
你可以通过点击:
man -Len -Pless\ +/^REDIRECTION bash
in a bash console ;-)
在bash控制台;-)
#4
65
The numbers refer to the file descriptors (fd).
数字指的是文件描述符(fd)。
- Zero is
stdin
- 0是stdin
- One is
stdout
- 一个是stdout
- Two is
stderr
- 二是stderr
2>&1
redirects fd 2 to 1.
>&1重定向fd2至1。
This works for any number of file descriptors if the program uses them.
如果程序使用它们,这对任何数量的文件描述符都有效。
You can look at /usr/include/unistd.h
if you forget them:
你可以看看/usr/include/unistd。如果你忘了它们:
/* Standard file descriptors. */
#define STDIN_FILENO 0 /* Standard input. */
#define STDOUT_FILENO 1 /* Standard output. */
#define STDERR_FILENO 2 /* Standard error output. */
That said I have written C tools that use non-standard file descriptors for custom logging so you don't see it unless you redirect it to a file or something.
这说明我已经编写了C工具,它使用非标准的文件描述符来进行自定义日志记录,所以除非您将其重定向到文件或其他文件,否则您不会看到它。
#5
48
That construct sends the standard error stream (stderr
) to the current location of standard output (stdout
) - this currency issue appears to have been neglected by the other answers.
该结构将标准错误流(stderr)发送到标准输出(stdout)的当前位置——这个货币问题似乎被其他答案忽略了。
You can redirect any output handle to another by using this method but it's most often used to channel stdout
and stderr
streams into a single stream for processing.
您可以使用此方法将任何输出句柄重定向到另一个,但它通常用于将stdout和stderr流导入到单个流中进行处理。
Some examples are:
一些例子:
# Look for ERROR string in both stdout and stderr.
foo 2>&1 | grep ERROR
# Run the less pager without stderr screwing up the output.
foo 2>&1 | less
# Send stdout/err to file (with append) and terminal.
foo 2>&1 |tee /dev/tty >>outfile
# Send stderr to normal location and stdout to file.
foo >outfile1 2>&1 >outfile2
Note that that last one will not direct stderr
to outfile2
- it redirects it to what stdout
was when the argument was encountered (outfile1
) and then redirects stdout
to outfile2
.
注意,最后一个不会直接将stderr直接输出到outfile2——它将它重定向到stdout在遇到参数时的情况(outfile1),然后重定向stdout到outfile2。
This allows some pretty sophisticated trickery.
这允许一些相当复杂的诡计。
#6
42
I found this brilliant post on redirection : All about redirections
我找到了这篇关于重定向的精彩文章:关于重定向。
Redirect both standard output and standard error to a file
将标准输出和标准错误重定向到文件。
$ command &>file
$命令与>文件
This one-liner uses the &> operator to redirect both output streams - stdout and stderr - from command to file. This is bash's shortcut for quickly redirecting both streams to the same destination.
这个一行程序使用&>操作符将输出流——stdout和stderr——从命令重定向到文件。这是bash的快捷方式,可以快速地将两个流重定向到同一个目的地。
Here is how the file descriptor table looks like after bash has redirected both streams:
下面是在bash对两个流进行重定向后文件描述符表的样子:
As you can see both stdout and stderr now point to file. So anything written to stdout and stderr gets written to file.
您可以看到stdout和stderr现在指向文件。因此写入stdout和stderr的任何东西都会被写入文件。
There are several ways to redirect both streams to the same destination. You can redirect each stream one after another:
有几种方法可以将两个流重定向到同一个目的地。您可以一个接一个地重定向每个流:
$ command >file 2>&1
命令>文件2 > & 1美元
This is a much more common way to redirect both streams to a file. First stdout is redirected to file, and then stderr is duplicated to be the same as stdout. So both streams end up pointing to file.
这是将两个流重定向到文件的更常见的方法。第一个stdout被重定向到文件,然后stderr被复制到与stdout相同。所以这两个流最后指向文件。
When bash sees several redirections it processes them from left to right. Let's go through the steps and see how that happens. Before running any commands bash's file descriptor table looks like this:
当bash看到几个重定向时,它从左到右处理它们。让我们看看这些步骤,看看会发生什么。在运行任何命令之前,bash的文件描述符表如下所示:
Now bash processes the first redirection >file. We've seen this before and it makes stdout point to file:
现在,bash处理第一个重定向>文件。我们之前见过,它使stdout指向文件:
Next bash sees the second redirection 2>&1. We haven't seen this redirection before. This one duplicates file descriptor 2 to be a copy of file descriptor 1 and we get:
下一个bash将看到第二个重定向2>&1。我们以前没见过这个重定向。这个复制文件描述符2是文件描述符1的副本,我们得到:
Both streams have been redirected to file.
这两个流都被重定向到文件。
However be careful here! Writing:
不过在这里要小心!写作:
command >file 2>&1
2 > & 1命令>文件
Is not the same as writing:
与写作不同:
$ command 2>&1 >file
$命令2 > & 1 >文件
The order of redirects matters in bash! This command redirects only the standard output to the file. The stderr will still print to the terminal. To understand why that happens, let's go through the steps again. So before running the command the file descriptor table looks like this:
在bash中重定向的顺序!此命令只将标准输出重定向到文件。stderr仍将打印到终端。要理解为什么会发生这样的事情,让我们再从头开始。在运行这个命令之前,文件描述符表是这样的:
Now bash processes redirections left to right. It first sees 2>&1 so it duplicates stderr to stdout. The file descriptor table becomes:
现在,bash进程从左到右进行重定向。它首先看到两个>和1,所以它复制stderr到stdout。文件描述符表变为:
Now bash sees the second redirect >file and it redirects stdout to file:
现在,bash看到第二个重定向>文件,并将stdout重定向到文件:
Do you see what happens here? Stdout now points to file but the stderr still points to the terminal! Everything that gets written to stderr still gets printed out to the screen! So be very, very careful with the order of redirects!
你知道这里发生了什么吗?Stdout现在指向文件,但是stderr仍然指向终端!所有被写入stderr的东西仍然会被打印到屏幕上!所以要非常非常小心的重定向!
Also note that in bash, writing this:
还要注意,在bash中,写这个:
$ command &>file
$命令与>文件
Is exactly the same as:
和:
$ command >&file
$命令>文件
#7
13
2>&1
is a POSIX shell construct. Here is a breakdown, token by token:
>&1是一个POSIX shell构造。这里是一个细分,令牌:
2
: "Standard error" output file descriptor.
2:“标准错误”输出文件描述符。
>&
: Duplicate an Output File Descriptor operator (a variant of Output Redirection operator >
). Given [x]>&[y]
, the file descriptor denoted by x
is made to be a copy of the output file descriptor y
.
>&:复制一个输出文件描述符操作符(输出重定向操作符>的一个变体)。给定[x]>&[y],用x表示的文件描述符是输出文件描述符y的拷贝。
1
"Standard output" output file descriptor.
“标准输出”输出文件描述符。
The expression 2>&1
copies file descriptor 1
to location 2
, so any output written to 2
("standard error") in the execution environment goes to the same file originally described by 1
("standard output").
表达式2>和1将文件描述符1复制到位置2,因此执行环境中写入到2(“标准错误”)的任何输出都指向最初由1(“标准输出”)描述的相同文件。
Further explanation:
进一步的解释:
File Descriptor: "A per-process unique, non-negative integer used to identify an open file for the purpose of file access."
文件描述符:“每个进程惟一的、非负整数,用于识别打开文件的文件访问目的。”
Standard output/error: Refer to the following note in the Redirection section of the shell documentation:
标准输出/错误:请参阅外壳文档的重定向部分中的以下说明:
Open files are represented by decimal numbers starting with zero. The largest possible value is implementation-defined; however, all implementations shall support at least 0 to 9, inclusive, for use by the application. These numbers are called "file descriptors". The values 0, 1, and 2 have special meaning and conventional uses and are implied by certain redirection operations; they are referred to as standard input, standard output, and standard error, respectively. Programs usually take their input from standard input, and write output on standard output. Error messages are usually written on standard error. The redirection operators can be preceded by one or more digits (with no intervening characters allowed) to designate the file descriptor number.
打开的文件用十进制数字表示,从零开始。最大的可能值是实现定义的;但是,所有的实现都应该至少支持0到9,包括应用程序。这些数字被称为“文件描述符”。值0、1和2具有特殊的意义和常规用法,并由某些重定向操作所暗示;它们分别被称为标准输入、标准输出和标准错误。程序通常从标准输入中获取输入,并在标准输出上写入输出。错误消息通常写在标准错误上。重定向操作符前面可以有一个或多个数字(不允许插入字符)来指定文件描述符编号。
#8
12
To answer your question: It takes any error output (normally sent to stderr) and writes it to standard output (stdout).
要回答您的问题:它接受任何错误输出(通常发送到stderr)并将其写入标准输出(stdout)。
This is helpful with, for example 'more' when you need paging for all output. Some programs like printing usage information into stderr.
这是有帮助的,例如,当您需要对所有输出进行分页时,“更多”。一些程序,如打印使用信息到stderr。
To help you remember
帮助你记住
- 1 = standard output (where programs print normal output)
- 1 =标准输出(程序输出正常输出)
- 2 = standard error (where programs print errors)
- 2 =标准错误(程序打印错误)
"2>&1" simply points everything sent to stderr, to stdout instead.
“2>&1”简单地把所有发送给stderr的东西都指向stdout。
I also recommend reading this post on error redirecting where this subject is covered in full detail.
我也建议阅读这篇关于错误重定向的文章,在这个主题的详细内容。
#9
12
2 is the Console standard error.
2是控制台标准错误。
1 is the Console standard output.
1是控制台标准输出。
This is the standard Unix, Windows also follows the POSIX. E.g. when you run
这是标准的Unix, Windows也遵循POSIX。例如,当您运行
perl test.pl 2>&1
The standard error is redirected to standard output, so you can see both outputs together.
标准错误被重定向到标准输出,因此您可以同时看到两个输出。
perl test.pl > debug.log 2>&1
After execution, you can see all the output, including errors, in the debug.log.
在执行之后,您可以在debug.log中看到所有输出,包括错误。
perl test.pl 1>out.log 2>err.log
Then standard output goes to out.log, and standard error to err.log.
然后输出标准输出。日志,以及error .log的标准错误。
I suggest you to try to understand these.
我建议你试着去理解这些。
#10
9
From a programmer's point of view, it means precisely this:
从程序员的角度来看,这意味着:
dup2(1, 2);
See the man page.
查看手册页。
Understanding that 2>&1
is a copy also explains why ...
理解2>&1是一份拷贝也解释了为什么…
command >file 2>&1
... is not the same as ...
…是不一样的…
command 2>&1 >file
The first will send both streams to file
, whereas the second will send errors to stdout
, and ordinary output into file
.
第一个将会将两个流发送到文件,而第二个会将错误发送到stdout,并将普通输出发送到文件中。
#11
4
People, always remember paxdiablo's hint about the current location of the redirection target... It is important.
人们,永远记住paxdiablo关于重定向目标的当前位置的提示………很重要。
My personal mnemonic for the 2>&1
operator is this:
我的两个>&1操作符的个人助记符是:
- Think of
&
as meaning'and'
or'add'
(the character is an ampers-and, isn't it?) - 你可以把它看成是“and”或“add”(字符是一个符号,不是吗?)
- So it becomes: 'redirect
2
(stderr) to where1
(stdout) already/currently is and add both streams'. - 所以它变成:'重定向2 (stderr)到1 (stdout)已经/当前的位置,并添加两条流。
The same mnemonic works for the other frequently used redirection too, 1>&2
:
同样的助记符也适用于其他常用的重定向,1>和2:
- Think of
&
meaningand
oradd
... (you get the idea about the ampersand, yes?) - 思考&意义,或添加…(你有关于&的想法,对吧?)
- So it becomes: 'redirect
1
(stdout) to where2
(stderr) already/currently is and add both streams'. - 所以它变成:'重定向1 (stdout)到2 (stderr)已经/当前的位置,并添加两个流。
And always remember: you have to read chains of redirections 'from the end', from right to left (not from left to right).
而且要记住:你必须读“从最后”到“从右到左”(不是从左到右)的重定向链。
#12
4
This is just like paasing the error to the stdout or terminal . i.e . cmd is not a command $cmd 2>filename cat filename command not found
这就像将错误传递给stdout或终端。我。e。cmd不是一个命令$cmd 2>文件名猫文件名命令没有找到。
The error sent to the file like that 2>&1 error sent to the terminal
发送到文件的错误,比如发送到终端的2>和1错误。
#13
4
Provided that /foo
does not exist on your system and /tmp
does…
如果您的系统中不存在/foo,那么/tmp…
$ ls -l /tmp /foo
will print the contents of /tmp
and print an error message for /foo
会打印/tmp的内容并打印错误消息吗?
$ ls -l /tmp /foo > /dev/null
will send the contents of /tmp
to /dev/null
and print an error message for /foo
将/tmp的内容发送到/dev/null,并打印错误消息给/foo ?
$ ls -l /tmp /foo 1> /dev/null
will do exactly the same (note the 1)
将会完全相同(注意1)
$ ls -l /tmp /foo 2> /dev/null
will print the contents of /tmp
and send the error message to /dev/null
打印/tmp的内容并将错误消息发送到/dev/null ?
$ ls -l /tmp /foo 1> /dev/null 2> /dev/null
will send both the listing as well as the error message to /dev/null
将清单和错误消息发送到/dev/null ?
$ ls -l /tmp /foo > /dev/null 2> &1
is shorthand
是简写
#14
2
Redirecting Input
Redirection of input causes the file whose name results from the expansion of word to be opened for reading on file descriptor n, or the standard input (file descriptor 0) if n is not specified.
输入的重定向导致了文件的名称,它的名称来自于为读取文件描述符n而打开的单词的扩展,或者如果没有指定n的标准输入(文件描述符0)。
The general format for redirecting input is:
重定向输入的一般格式为:
[n]<word
Redirecting Output
Redirection of output causes the file whose name results from the expansion of word to be opened for writing on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size.
如果没有指定n,则输出的重定向导致文件的名称由扩展的word扩展到文件描述符n,或标准输出(文件描述符1)。如果文件不存在,就创建;如果存在,则将其截断为零大小。
The general format for redirecting output is:
重定向输出的一般格式为:
[n]>word
Moving File Descriptors
Moving File Descriptors The redirection operator
移动文件描述符,重定向操作符。
[n]<&digit-
moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.
将文件描述符数字移动到文件描述符n,或者如果没有指定n,则标准输入(文件描述符0)。数字在复制到n后关闭。
Similarly, the redirection operator
同样,重定向操作符
[n]>&digit-
moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is not specified.
将文件描述符数字移动到文件描述符n,或者如果没有指定n,则将标准输出(文件描述符1)移动。
ref:
man bash
type /^REDIRECT
to locate to the redirection
section , learn more..
男人bash类型/ ^定向定位定向部分,学习更多. .
a online version here:
http://www.gnu.org/software/bash/manual/bashref.html#Redirections
这里有一个在线版本:http://www.gnu.org/software/bash/manual/bashref.html#重定向。
ps:
lots of time, man
was the powerful tool to learn linux
很多时候,人类是学习linux的强大工具。
#15
0
0 for input, 1 for stdout and 2 for stderr.
输入为0,stdout为1,stderr为2。
One Tip: somecmd >1.txt 2>&1
is correct, while somecmd 2>&1 >1.txt
is totally wrong with no effect!
一个提示:somecmd > 1。txt >&1是正确的,而somecmd 2>&1 >1。txt是完全错误的,没有效果!
#1
1887
File descriptor 1 is the standard output (stdout
).
File descriptor 2 is the standard error (stderr
).
文件描述符1是标准输出(stdout)。文件描述符2是标准错误(stderr)。
Here is one way to remember this construct (although it is not entirely accurate): at first, 2>1
may look like a good way to redirect stderr
to stdout
. However, it will actually be interpreted as "redirect stderr
to a file named 1
". &
indicates that what follows is a file descriptor and not a filename. So the construct becomes: 2>&1
.
这里有一种方法来记住这个结构(尽管它不是完全正确的):首先,2>1看起来是一个将stderr重定向到stdout的好方法。但是,它实际上会被解释为“将stderr重定向到一个名为1的文件”。表示下面是一个文件描述符而不是文件名。所以构造变成:2>和1。
#2
463
echo test > afile.txt
..redirects stdout to afile.txt
. This is the same as doing..
. .afile.txt重定向标准输出。这跟做一样。
echo test 1> afile.txt
To redirect stderr, you do..
要重定向stderr,你需要。
echo test 2> afile.txt
>&
is the syntax to redirect a stream to another file descriptor - 0 is stdin. 1 is stdout. 2 is stderr.
>&是将流重定向到另一个文件描述符的语法—0是stdin。1是stdout。2是stderr。
You can redirect stdout to stderr by doing..
你可以把stdout重定向到stderr。
echo test 1>&2 # or echo test >&2
..or vice versa:
. .反之亦然:
echo test 2>&1
So, in short.. 2>
redirects stderr to an (unspecified) file, appending &1
redirects stderr to stdout
所以,简而言之. .>将stderr重定向到一个(未指定的)文件,appending &1将stderr重定向到stdout。
#3
249
Some tricks about redirection
Some syntax particularity about this may have important behaviours. There is some little samples about redirections, STDERR
, STDOUT
and arguments ordering.
一些语法的特殊性可能有重要的行为。有一些关于重定向、STDERR、STDOUT和参数排序的小样本。
1 - Overwritting or appending?
Symbole >
mean redirection.
作品喻示>的意思是重定向。
-
>
mean send to as a whole completed file, overwriting target if exist (seenoclobber
bash feature at #3 later). - >的意思是发送到一个完整的文件,如果存在的话覆盖目标(参见第3条的noclobber bash特性)。
-
>>
mean send in addition to would append to target if exist. - >>的意思是,如果存在的话,将附加到目标。
Any case, the file would be created if they not exist.
任何情况下,如果文件不存在,文件就会被创建。
2 - The shell command line is order dependant!!
For testing this, we need a simple command which will send something on both outputs:
为了测试这个,我们需要一个简单的命令,它将在两个输出上发送一些东西:
$ ls -ld /tmp /tnt
ls: cannot access /tnt: No such file or directory
drwxrwxrwt 118 root root 196608 Jan 7 11:49 /tmp
$ ls -ld /tmp /tnt >/dev/null
ls: cannot access /tnt: No such file or directory
$ ls -ld /tmp /tnt 2>/dev/null
drwxrwxrwt 118 root root 196608 Jan 7 11:49 /tmp
(Expecting you don't have a directory named /tnt
, of course ;). Well, we have it!!
(当然,希望您没有一个名为/tnt的目录;)嗯,我们有它! !
So lets see:
所以让我们看看:
$ ls -ld /tmp /tnt >/dev/null
ls: cannot access /tnt: No such file or directory
$ ls -ld /tmp /tnt >/dev/null 2>&1
$ ls -ld /tmp /tnt 2>&1 >/dev/null
ls: cannot access /tnt: No such file or directory
The last command line dump STDERR
to the console, it seem not to be the expected behaviour... But...
最后一个命令行转储STDERR到控制台,它似乎不是预期的行为…但是…
If you want to make some post filtering about one ouput, the other or both:
如果你想要对一个ouput进行过滤,另一个或两者都要:
$ ls -ld /tmp /tnt | sed 's/^.*$/<-- & --->/'
ls: cannot access /tnt: No such file or directory
<-- drwxrwxrwt 118 root root 196608 Jan 7 12:02 /tmp --->
$ ls -ld /tmp /tnt 2>&1 | sed 's/^.*$/<-- & --->/'
<-- ls: cannot access /tnt: No such file or directory --->
<-- drwxrwxrwt 118 root root 196608 Jan 7 12:02 /tmp --->
$ ls -ld /tmp /tnt >/dev/null | sed 's/^.*$/<-- & --->/'
ls: cannot access /tnt: No such file or directory
$ ls -ld /tmp /tnt >/dev/null 2>&1 | sed 's/^.*$/<-- & --->/'
$ ls -ld /tmp /tnt 2>&1 >/dev/null | sed 's/^.*$/<-- & --->/'
<-- ls: cannot access /tnt: No such file or directory --->
Notice that the last command line in this paragraph is exactly same as in previous paraghaph, where I wrote seem not to be the expected behaviour (so, this could even be an expected behaviour).
请注意,这一段中的最后一条命令行与前面的段落中所写的完全相同,我所写的似乎并不是预期的行为(因此,这甚至可能是一种预期的行为)。
Well there is a little tricks about redirections, for doing different operation on both ouputs:
这里有一些关于重定向的小技巧,用于在两个输出上做不同的操作:
$ ( ls -ld /tmp /tnt | sed 's/^/O: /' >&9 ) 9>&2 2>&1 | sed 's/^/E: /'
O: drwxrwxrwt 118 root root 196608 Jan 7 12:13 /tmp
E: ls: cannot access /tnt: No such file or directory
Nota: &9
descriptor would occur spontaneously because of ) 9>&2
.
Nota:和9描述符会自发产生,因为)9>&2。
Addendum: nota! With new version of bash (>4.0
) there is a new feature and more sexy syntax for doing this kind of things:
附录:背板!有了新版本的bash(>4.0),有了一种新的特性和更性感的语法来做这类事情:
$ ls -ld /tmp /tnt 2> >(sed 's/^/E: /') > >(sed 's/^/O: /')
O: drwxrwxrwt 17 root root 28672 Nov 5 23:00 /tmp
E: ls: cannot access /tnt: No such file or directory
And finaly for such a cascading output formatting:
对于这样的级联输出格式,finaly:
$ ((ls -ld /tmp /tnt |sed 's/^/O: /' >&9 ) 2>&1 |sed 's/^/E: /') 9>&1| cat -n
1 O: drwxrwxrwt 118 root root 196608 Jan 7 12:29 /tmp
2 E: ls: cannot access /tnt: No such file or directory
Addendum: nota! Same new syntax, in both ways:
附录:背板!同样的新语法,在两方面:
$ cat -n <(ls -ld /tmp /tnt 2> >(sed 's/^/E: /') > >(sed 's/^/O: /'))
1 O: drwxrwxrwt 17 root root 28672 Nov 5 23:00 /tmp
2 E: ls: cannot access /tnt: No such file or directory
Where STDOUT
go through a specific filter, STDERR
to another and finally both outputs merged go through a third command filter.
STDOUT通过一个特定的过滤器,STDERR到另一个,最后两个输出合并通过第三个命令过滤器。
3 - A word about noclobber
option and >|
syntax
That's about overwritting:
这是关于overwritting:
While set -o noclobber
instruct bash to not overwrite any existing file, the >|
syntax let you pass through this limitation:
当set -o noclobber指示bash不要覆盖任何现有文件时,>|语法允许您通过这个限制:
$ testfile=$(mktemp /tmp/testNoClobberDate-XXXXXX)
$ date > $testfile ; cat $testfile
Mon Jan 7 13:18:15 CET 2013
$ date > $testfile ; cat $testfile
Mon Jan 7 13:18:19 CET 2013
$ date > $testfile ; cat $testfile
Mon Jan 7 13:18:21 CET 2013
File is overwritted each time, well now:
文件每次都被重写,现在:
$ set -o noclobber
$ date > $testfile ; cat $testfile
bash: /tmp/testNoClobberDate-WW1xi9: cannot overwrite existing file
Mon Jan 7 13:18:21 CET 2013
$ date > $testfile ; cat $testfile
bash: /tmp/testNoClobberDate-WW1xi9: cannot overwrite existing file
Mon Jan 7 13:18:21 CET 2013
Pass through with >|
:
通过与> |:
$ date >| $testfile ; cat $testfile
Mon Jan 7 13:18:58 CET 2013
$ date >| $testfile ; cat $testfile
Mon Jan 7 13:19:01 CET 2013
Unsetting this option and/or inquiring if already set.
取消此选项和/或询问是否已设置。
$ set -o | grep noclobber
noclobber on
$ set +o noclobber
$ set -o | grep noclobber
noclobber off
$ date > $testfile ; cat $testfile
Mon Jan 7 13:24:27 CET 2013
$ rm $testfile
4 - Last trick and more...
For redirecting both output from a given command, we see that a right syntax could be:
为了从给定的命令中重定向两个输出,我们可以看到一个正确的语法:
$ ls -ld /tmp /tnt >/dev/null 2>&1
for this special case, there is a shortcut syntax: &>
... or >&
对于这个特殊情况,有一个快捷语法:&>…或> &
$ ls -ld /tmp /tnt &>/dev/null
$ ls -ld /tmp /tnt >&/dev/null
Nota: if 2>&1
exist, 1>&2
is a correct syntaxe too:
如果2个>和1存在,1>和2也是正确的syntaxe:
$ ls -ld /tmp /tnt 2>/dev/null 1>&2
4b- Now, I will let you think about:
$ ls -ld /tmp /tnt 2>&1 1>&2 | sed -e s/^/++/
++/bin/ls: cannot access /tnt: No such file or directory
++drwxrwxrwt 193 root root 196608 Feb 9 11:08 /tmp/
$ ls -ld /tmp /tnt 1>&2 2>&1 | sed -e s/^/++/
/bin/ls: cannot access /tnt: No such file or directory
drwxrwxrwt 193 root root 196608 Feb 9 11:08 /tmp/
4c- If you're interested in more informations
you could Read The Fine Manual by hitting:
你可以通过点击:
man -Len -Pless\ +/^REDIRECTION bash
in a bash console ;-)
在bash控制台;-)
#4
65
The numbers refer to the file descriptors (fd).
数字指的是文件描述符(fd)。
- Zero is
stdin
- 0是stdin
- One is
stdout
- 一个是stdout
- Two is
stderr
- 二是stderr
2>&1
redirects fd 2 to 1.
>&1重定向fd2至1。
This works for any number of file descriptors if the program uses them.
如果程序使用它们,这对任何数量的文件描述符都有效。
You can look at /usr/include/unistd.h
if you forget them:
你可以看看/usr/include/unistd。如果你忘了它们:
/* Standard file descriptors. */
#define STDIN_FILENO 0 /* Standard input. */
#define STDOUT_FILENO 1 /* Standard output. */
#define STDERR_FILENO 2 /* Standard error output. */
That said I have written C tools that use non-standard file descriptors for custom logging so you don't see it unless you redirect it to a file or something.
这说明我已经编写了C工具,它使用非标准的文件描述符来进行自定义日志记录,所以除非您将其重定向到文件或其他文件,否则您不会看到它。
#5
48
That construct sends the standard error stream (stderr
) to the current location of standard output (stdout
) - this currency issue appears to have been neglected by the other answers.
该结构将标准错误流(stderr)发送到标准输出(stdout)的当前位置——这个货币问题似乎被其他答案忽略了。
You can redirect any output handle to another by using this method but it's most often used to channel stdout
and stderr
streams into a single stream for processing.
您可以使用此方法将任何输出句柄重定向到另一个,但它通常用于将stdout和stderr流导入到单个流中进行处理。
Some examples are:
一些例子:
# Look for ERROR string in both stdout and stderr.
foo 2>&1 | grep ERROR
# Run the less pager without stderr screwing up the output.
foo 2>&1 | less
# Send stdout/err to file (with append) and terminal.
foo 2>&1 |tee /dev/tty >>outfile
# Send stderr to normal location and stdout to file.
foo >outfile1 2>&1 >outfile2
Note that that last one will not direct stderr
to outfile2
- it redirects it to what stdout
was when the argument was encountered (outfile1
) and then redirects stdout
to outfile2
.
注意,最后一个不会直接将stderr直接输出到outfile2——它将它重定向到stdout在遇到参数时的情况(outfile1),然后重定向stdout到outfile2。
This allows some pretty sophisticated trickery.
这允许一些相当复杂的诡计。
#6
42
I found this brilliant post on redirection : All about redirections
我找到了这篇关于重定向的精彩文章:关于重定向。
Redirect both standard output and standard error to a file
将标准输出和标准错误重定向到文件。
$ command &>file
$命令与>文件
This one-liner uses the &> operator to redirect both output streams - stdout and stderr - from command to file. This is bash's shortcut for quickly redirecting both streams to the same destination.
这个一行程序使用&>操作符将输出流——stdout和stderr——从命令重定向到文件。这是bash的快捷方式,可以快速地将两个流重定向到同一个目的地。
Here is how the file descriptor table looks like after bash has redirected both streams:
下面是在bash对两个流进行重定向后文件描述符表的样子:
As you can see both stdout and stderr now point to file. So anything written to stdout and stderr gets written to file.
您可以看到stdout和stderr现在指向文件。因此写入stdout和stderr的任何东西都会被写入文件。
There are several ways to redirect both streams to the same destination. You can redirect each stream one after another:
有几种方法可以将两个流重定向到同一个目的地。您可以一个接一个地重定向每个流:
$ command >file 2>&1
命令>文件2 > & 1美元
This is a much more common way to redirect both streams to a file. First stdout is redirected to file, and then stderr is duplicated to be the same as stdout. So both streams end up pointing to file.
这是将两个流重定向到文件的更常见的方法。第一个stdout被重定向到文件,然后stderr被复制到与stdout相同。所以这两个流最后指向文件。
When bash sees several redirections it processes them from left to right. Let's go through the steps and see how that happens. Before running any commands bash's file descriptor table looks like this:
当bash看到几个重定向时,它从左到右处理它们。让我们看看这些步骤,看看会发生什么。在运行任何命令之前,bash的文件描述符表如下所示:
Now bash processes the first redirection >file. We've seen this before and it makes stdout point to file:
现在,bash处理第一个重定向>文件。我们之前见过,它使stdout指向文件:
Next bash sees the second redirection 2>&1. We haven't seen this redirection before. This one duplicates file descriptor 2 to be a copy of file descriptor 1 and we get:
下一个bash将看到第二个重定向2>&1。我们以前没见过这个重定向。这个复制文件描述符2是文件描述符1的副本,我们得到:
Both streams have been redirected to file.
这两个流都被重定向到文件。
However be careful here! Writing:
不过在这里要小心!写作:
command >file 2>&1
2 > & 1命令>文件
Is not the same as writing:
与写作不同:
$ command 2>&1 >file
$命令2 > & 1 >文件
The order of redirects matters in bash! This command redirects only the standard output to the file. The stderr will still print to the terminal. To understand why that happens, let's go through the steps again. So before running the command the file descriptor table looks like this:
在bash中重定向的顺序!此命令只将标准输出重定向到文件。stderr仍将打印到终端。要理解为什么会发生这样的事情,让我们再从头开始。在运行这个命令之前,文件描述符表是这样的:
Now bash processes redirections left to right. It first sees 2>&1 so it duplicates stderr to stdout. The file descriptor table becomes:
现在,bash进程从左到右进行重定向。它首先看到两个>和1,所以它复制stderr到stdout。文件描述符表变为:
Now bash sees the second redirect >file and it redirects stdout to file:
现在,bash看到第二个重定向>文件,并将stdout重定向到文件:
Do you see what happens here? Stdout now points to file but the stderr still points to the terminal! Everything that gets written to stderr still gets printed out to the screen! So be very, very careful with the order of redirects!
你知道这里发生了什么吗?Stdout现在指向文件,但是stderr仍然指向终端!所有被写入stderr的东西仍然会被打印到屏幕上!所以要非常非常小心的重定向!
Also note that in bash, writing this:
还要注意,在bash中,写这个:
$ command &>file
$命令与>文件
Is exactly the same as:
和:
$ command >&file
$命令>文件
#7
13
2>&1
is a POSIX shell construct. Here is a breakdown, token by token:
>&1是一个POSIX shell构造。这里是一个细分,令牌:
2
: "Standard error" output file descriptor.
2:“标准错误”输出文件描述符。
>&
: Duplicate an Output File Descriptor operator (a variant of Output Redirection operator >
). Given [x]>&[y]
, the file descriptor denoted by x
is made to be a copy of the output file descriptor y
.
>&:复制一个输出文件描述符操作符(输出重定向操作符>的一个变体)。给定[x]>&[y],用x表示的文件描述符是输出文件描述符y的拷贝。
1
"Standard output" output file descriptor.
“标准输出”输出文件描述符。
The expression 2>&1
copies file descriptor 1
to location 2
, so any output written to 2
("standard error") in the execution environment goes to the same file originally described by 1
("standard output").
表达式2>和1将文件描述符1复制到位置2,因此执行环境中写入到2(“标准错误”)的任何输出都指向最初由1(“标准输出”)描述的相同文件。
Further explanation:
进一步的解释:
File Descriptor: "A per-process unique, non-negative integer used to identify an open file for the purpose of file access."
文件描述符:“每个进程惟一的、非负整数,用于识别打开文件的文件访问目的。”
Standard output/error: Refer to the following note in the Redirection section of the shell documentation:
标准输出/错误:请参阅外壳文档的重定向部分中的以下说明:
Open files are represented by decimal numbers starting with zero. The largest possible value is implementation-defined; however, all implementations shall support at least 0 to 9, inclusive, for use by the application. These numbers are called "file descriptors". The values 0, 1, and 2 have special meaning and conventional uses and are implied by certain redirection operations; they are referred to as standard input, standard output, and standard error, respectively. Programs usually take their input from standard input, and write output on standard output. Error messages are usually written on standard error. The redirection operators can be preceded by one or more digits (with no intervening characters allowed) to designate the file descriptor number.
打开的文件用十进制数字表示,从零开始。最大的可能值是实现定义的;但是,所有的实现都应该至少支持0到9,包括应用程序。这些数字被称为“文件描述符”。值0、1和2具有特殊的意义和常规用法,并由某些重定向操作所暗示;它们分别被称为标准输入、标准输出和标准错误。程序通常从标准输入中获取输入,并在标准输出上写入输出。错误消息通常写在标准错误上。重定向操作符前面可以有一个或多个数字(不允许插入字符)来指定文件描述符编号。
#8
12
To answer your question: It takes any error output (normally sent to stderr) and writes it to standard output (stdout).
要回答您的问题:它接受任何错误输出(通常发送到stderr)并将其写入标准输出(stdout)。
This is helpful with, for example 'more' when you need paging for all output. Some programs like printing usage information into stderr.
这是有帮助的,例如,当您需要对所有输出进行分页时,“更多”。一些程序,如打印使用信息到stderr。
To help you remember
帮助你记住
- 1 = standard output (where programs print normal output)
- 1 =标准输出(程序输出正常输出)
- 2 = standard error (where programs print errors)
- 2 =标准错误(程序打印错误)
"2>&1" simply points everything sent to stderr, to stdout instead.
“2>&1”简单地把所有发送给stderr的东西都指向stdout。
I also recommend reading this post on error redirecting where this subject is covered in full detail.
我也建议阅读这篇关于错误重定向的文章,在这个主题的详细内容。
#9
12
2 is the Console standard error.
2是控制台标准错误。
1 is the Console standard output.
1是控制台标准输出。
This is the standard Unix, Windows also follows the POSIX. E.g. when you run
这是标准的Unix, Windows也遵循POSIX。例如,当您运行
perl test.pl 2>&1
The standard error is redirected to standard output, so you can see both outputs together.
标准错误被重定向到标准输出,因此您可以同时看到两个输出。
perl test.pl > debug.log 2>&1
After execution, you can see all the output, including errors, in the debug.log.
在执行之后,您可以在debug.log中看到所有输出,包括错误。
perl test.pl 1>out.log 2>err.log
Then standard output goes to out.log, and standard error to err.log.
然后输出标准输出。日志,以及error .log的标准错误。
I suggest you to try to understand these.
我建议你试着去理解这些。
#10
9
From a programmer's point of view, it means precisely this:
从程序员的角度来看,这意味着:
dup2(1, 2);
See the man page.
查看手册页。
Understanding that 2>&1
is a copy also explains why ...
理解2>&1是一份拷贝也解释了为什么…
command >file 2>&1
... is not the same as ...
…是不一样的…
command 2>&1 >file
The first will send both streams to file
, whereas the second will send errors to stdout
, and ordinary output into file
.
第一个将会将两个流发送到文件,而第二个会将错误发送到stdout,并将普通输出发送到文件中。
#11
4
People, always remember paxdiablo's hint about the current location of the redirection target... It is important.
人们,永远记住paxdiablo关于重定向目标的当前位置的提示………很重要。
My personal mnemonic for the 2>&1
operator is this:
我的两个>&1操作符的个人助记符是:
- Think of
&
as meaning'and'
or'add'
(the character is an ampers-and, isn't it?) - 你可以把它看成是“and”或“add”(字符是一个符号,不是吗?)
- So it becomes: 'redirect
2
(stderr) to where1
(stdout) already/currently is and add both streams'. - 所以它变成:'重定向2 (stderr)到1 (stdout)已经/当前的位置,并添加两条流。
The same mnemonic works for the other frequently used redirection too, 1>&2
:
同样的助记符也适用于其他常用的重定向,1>和2:
- Think of
&
meaningand
oradd
... (you get the idea about the ampersand, yes?) - 思考&意义,或添加…(你有关于&的想法,对吧?)
- So it becomes: 'redirect
1
(stdout) to where2
(stderr) already/currently is and add both streams'. - 所以它变成:'重定向1 (stdout)到2 (stderr)已经/当前的位置,并添加两个流。
And always remember: you have to read chains of redirections 'from the end', from right to left (not from left to right).
而且要记住:你必须读“从最后”到“从右到左”(不是从左到右)的重定向链。
#12
4
This is just like paasing the error to the stdout or terminal . i.e . cmd is not a command $cmd 2>filename cat filename command not found
这就像将错误传递给stdout或终端。我。e。cmd不是一个命令$cmd 2>文件名猫文件名命令没有找到。
The error sent to the file like that 2>&1 error sent to the terminal
发送到文件的错误,比如发送到终端的2>和1错误。
#13
4
Provided that /foo
does not exist on your system and /tmp
does…
如果您的系统中不存在/foo,那么/tmp…
$ ls -l /tmp /foo
will print the contents of /tmp
and print an error message for /foo
会打印/tmp的内容并打印错误消息吗?
$ ls -l /tmp /foo > /dev/null
will send the contents of /tmp
to /dev/null
and print an error message for /foo
将/tmp的内容发送到/dev/null,并打印错误消息给/foo ?
$ ls -l /tmp /foo 1> /dev/null
will do exactly the same (note the 1)
将会完全相同(注意1)
$ ls -l /tmp /foo 2> /dev/null
will print the contents of /tmp
and send the error message to /dev/null
打印/tmp的内容并将错误消息发送到/dev/null ?
$ ls -l /tmp /foo 1> /dev/null 2> /dev/null
will send both the listing as well as the error message to /dev/null
将清单和错误消息发送到/dev/null ?
$ ls -l /tmp /foo > /dev/null 2> &1
is shorthand
是简写
#14
2
Redirecting Input
Redirection of input causes the file whose name results from the expansion of word to be opened for reading on file descriptor n, or the standard input (file descriptor 0) if n is not specified.
输入的重定向导致了文件的名称,它的名称来自于为读取文件描述符n而打开的单词的扩展,或者如果没有指定n的标准输入(文件描述符0)。
The general format for redirecting input is:
重定向输入的一般格式为:
[n]<word
Redirecting Output
Redirection of output causes the file whose name results from the expansion of word to be opened for writing on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size.
如果没有指定n,则输出的重定向导致文件的名称由扩展的word扩展到文件描述符n,或标准输出(文件描述符1)。如果文件不存在,就创建;如果存在,则将其截断为零大小。
The general format for redirecting output is:
重定向输出的一般格式为:
[n]>word
Moving File Descriptors
Moving File Descriptors The redirection operator
移动文件描述符,重定向操作符。
[n]<&digit-
moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.
将文件描述符数字移动到文件描述符n,或者如果没有指定n,则标准输入(文件描述符0)。数字在复制到n后关闭。
Similarly, the redirection operator
同样,重定向操作符
[n]>&digit-
moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is not specified.
将文件描述符数字移动到文件描述符n,或者如果没有指定n,则将标准输出(文件描述符1)移动。
ref:
man bash
type /^REDIRECT
to locate to the redirection
section , learn more..
男人bash类型/ ^定向定位定向部分,学习更多. .
a online version here:
http://www.gnu.org/software/bash/manual/bashref.html#Redirections
这里有一个在线版本:http://www.gnu.org/software/bash/manual/bashref.html#重定向。
ps:
lots of time, man
was the powerful tool to learn linux
很多时候,人类是学习linux的强大工具。
#15
0
0 for input, 1 for stdout and 2 for stderr.
输入为0,stdout为1,stderr为2。
One Tip: somecmd >1.txt 2>&1
is correct, while somecmd 2>&1 >1.txt
is totally wrong with no effect!
一个提示:somecmd > 1。txt >&1是正确的,而somecmd 2>&1 >1。txt是完全错误的,没有效果!