Understand a dialect in the Linux Shell: 2>&1

Source: Internet
Author: User

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:

    • STDOUT is 1

    • STDERR is 2

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:

    • StdOut Redirect to /dev/null

    • stderr Redirect to &1

/dev/nullis empty device meaning, redirect to the empty device, that is, the output information is not.

&1Represents 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 -> 1stderr -> 2

    • command > outputIt's command 1> output shorthand.

    • &文件描述符is a value that refers to a file descriptor

    • 2>&1The location of the error output is the same as the standard output

Understand a dialect in the Linux Shell: 2>&1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.