Shell standard output error >/dev/null 2>&1 analysis in Linux

Source: Internet
Author: User

The shell may often see: >/dev/null 2>&1

Eg:sudo kill-9 ' ps-elf |grep-v grep|grep $1|awk ' {print $} ' 1>/dev/null 2>/dev/null

The result of the command can be defined as the output in the form of%>

/dev/null represents empty device files

> represents where to redirect, for example: echo "123" >/home/123.txt
1 indicates stdout standard output, the system default is 1, so ">/dev/null" is equivalent to "1>/dev/null"
2 indicates stderr standard error
& means equal to, 2>&1, indicating 2 output redirection equals 1

Then the statement of the title of this article:

1>/dev/null first indicates that the standard output is redirected to an empty device file, that is, not outputting any information to the terminal, which means no information is displayed.

2>&1 then, standard error output redirection is equivalent to standard output, because the standard output is redirected to an empty device file, so the normal error output is redirected to the empty device file.

2>&1 written in the back of the reason

Format: command > file 2>&1 = = Command 1> file 2>&1

First, command > file redirects standard output to file, 2>&1 is the standard error copy of standard output, which is also redirected to file, and the final result is that standard output and errors are redirected to file.

If changed to: command 2>&1 >file

The 2>&1 standard error copies the behavior of the standard output, but at this point the standard output is output to the terminal. The standard output is redirected to file after >file, but the standard error still retains the previous setting, that is, keeping the output to the terminal.

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.