Understand a dialect in the Linux Shell: 2>&12016-11-14 Du Yishu Objective
When using Linux commands or shell programming, this usage often encounters 2>&1
If it is just beginning to touch Linux, this thing really does not understand, because he has no intuitive meaning, not like a command, such as cp
copy
shorthand, very good remember.
I used to use Linux on this thing confused for a while, today just see an article introduced him, feel it is necessary to summarize, share to still do not understand this dialect of friends.
Let's look at a command example and then analyze how he works:
ls foo > /dev/null 2>&1
First, you need to understand two basic concepts: I/O redirection, file descriptor
I/O redirection
redirect the function is to send the output of one command to another place.
For example, using cat
a command to view a file, the contents of the file will be printed to the screen:
$ cat foo.txtfoobarbaz
At this point, the screen is the standard output of the command ("stdout") position.
We can send the contents of the file to other places, for example, redirect to the output.txt
file:
$ cat foo.txt > output.txt$ cat output.txtfoobarbaz
The first one cat
used >
to stdout
change the position to another file.
Look at an example to see a nonexistent file:
$ cat nop.txt > output.txtcat: nop.txt: No such file or directory
Why does the error message here appear on the screen instead of being sent to? output.txt
This involves another location: The standard error output standard error 【stderr】
.
$ cat nop.txt > output.txt
This command locates the stdout
file and does not have a defined stderr
location, so the error message is displayed to the default location: screen.
File descriptor
All files in Linux, each file has a file descriptor, the value is a positive integer.
Therefore, the standard output stdout
and the standard error output stderr
also have their own file descriptors:
Comprehensive
$ cat foo.txt > output.txt
This is actually the case:
$ cat foo.txt 1> output.txt
It is stdout
output.txt
>
1>
The shorthand that points to it.
So the output of the redirect error message should look like this:
$ cat nop.txt 2> error.txt$ cat error.txtcat: nop.txt: No such file or directory
Even a command is connected:
$ cat foo.txt > output.txt 2> error.txt
The standard output stdout
and standard error output are also redirected stderr
.
Review the beginning of the command:
ls foo > /dev/null 2>&1
Now it's basically understandable:
/dev/null
is empty device meaning, redirect to the empty device, that is, the output information is not.
&1
Represents the value of the file descriptor 1 , which is the standard output value, then the 2>&1
standard error Output is the same as the standard output , is also redirected to an empty device.
So this command means: both the correct information and the error message are not displayed.
Summary
Several key points:
Two outputs: standard output (stdout normal information), standard error output (stderr error message)
>
Can change the position of the output
File descriptor, stdout -> 1
stderr -> 2
command > output
It's command 1> output
shorthand.
&文件描述符
is a value that refers to a file descriptor
2>&1
The location of the error output is the same as the standard output
Understand a dialect in the Linux Shell: 2>&1