>/dev/null 2>&1 in the shell

Source: Internet
Author: User


This article goes from http://www.kissyu.org/background


We can often find such statements in a shell script>/dev/null 2>&1. Before I did not go into the role of this command, copy replicable, until last week I wrote this order accidentally2>&1 >/dev/null, a little problem, I began to understand the "mystery" behind the order.


Shell Redirection Introduction


Just like the program we write, a program handles the external input and outputs the result to the specified location. In an interactive program, enter the keyboard and mouse from the user, output the results to the user's screen, or even play the device. For some programs that run in the background, input may come from external files, and the results of the operations are usually written to other files. And the program in the process of running, there will be some critical information, such as the exception stack, external interface calls, etc., all of these will be written in the log file.



The shell script is the same, but we generally use the shell command more often than the keyboard input, and then on the screen to see the results of the command execution. If, in some cases, we need to store the execution result of the shell command in a file, then we need to use the redirect function of the input and output.


File descriptor


When you execute a shell command, 3 files are opened by default, and each file has a corresponding file descriptor to facilitate our use:


type File Descriptor Default Condition corresponding file handle location
Standard input 0 Getting input from the keyboard /proc/slef/fd/0
Standard output 1 Output to screen (i.e. console) /proc/slef/fd/1
Bad output (Error outputs) 2 Output to screen (i.e. console) /proc/slef/fd/2


So we usually get input from the keyboard by default in executing the shell command, and output the result to the console. But we can redirect the input and output by changing the default point of the file descriptor. For example, if we point 1 to a file, the standard output will be output to the file.


Output redirection


The use of output redirection is simple, and some basic commands are as follows:


Command Introduction
Command >filename REDIRECT standard output to a new file
Command 1>filename Ditto
Command >>filename Append the standard output to the file
Command 1>>filename Ditto
Command 2>filename redirect standard errors to a new file
Command 2>>filename Append the standard error to the new file


We use>or redirect>>the output. The left side of the symbol represents the file descriptor, and if not, represents 1, which is the standard output, and the right side of the symbol can be a file or an output device. When used>, it is determined that the right file does not exist, if it exists, it is deleted first, then create a new file, does not exist, it is created directly. However, when>>an append is used, the file that already exists is not deleted.



To better understand output redirection and feel the "glamour" of redirection, let's look at the following example: we create a test directory with only one a.txt file under the directory.


12345678 # tree.└──a.txt 0 directories, 1 file# LS a.txt B.TXTLS: Unable to access b.txt: No file or directory A.txt


After we executels a.txt b.txt, there are two output, which is thels: 无法访问b.txt: 没有那个文件或目录error output,a.txtis the standard output.


123456789 # ls a.txt b.txt 1>outls: Inaccessible b.txt: No file or directory # cat outa.txt# ls a.txt b.txt >>outls: Unreachable b.txt: No file or directory # cat O Uta.txta.txt


In the above command, we redirect the original standard output to the out file, so the console is left with only error hints. And when the append operation is performed, the contents of the out file are not emptied, but there is one morea.txt.



In the same vein, we can redirect the error output to a file:


123456789 # ls a.txt b.txt 2>erra.txt# cat Errls: Unable to access b.txt: No file or directory # ls a.txt b.txt >out 2>err# cat outa.txt# cat Errls: Unable to access b.txt: No file or directory


See here, friends may find>out 2>errand we mentioned in the beginning of a>/dev/null 2>&1very similar, don't worry, this will be later.


Input redirect


After understanding the output redirection, it is much easier to understand the input redirection. The basic commands for input redirection are as follows:


Command Introduction
Command <filename Use the filename file as the standard input
Command 0<filename Ditto
Command <<delimiter Read in from standard input until delimiter delimiter is encountered


We use a redirect to the<input, and if there is no write value to the left of the symbol, then the default is 0.



This time we take the cat command as an example, if the cat does not follow the file name, it is the function of the standard input (such as the keyboard) echo to the standard output (such as the screen):


12345 # Cat123123testtest


We can use the input redirection to write the characters we typed on the keyboard to the file. We need to use CTRL + C to end the input:


1234567 # Cat >out123test^c# Cat Out123test


Well, at this point we feel tired on the keyboard, or just let the cat read a file. Then we need to use input redirection:


1234567 # cat inputaaa111# cat >out <input# Cat outaaa111


Something magical happened, and the contents of the out file were replaced with the contents of the input file. So<<what's the effect? Let's look again:


1234567 # cat >out <<end> 123> test> end# cat Out123test


We see that when we have finished typing andcat >out <<endthen hit enter, the command does not end, and the cat command waits for you to enter data for it, as it was at the beginning. Then when we typed itend, the Cat command ended.endthe previously entered characters have been written to the out file. This is the function of the input separator.


Advanced Usage Redirect Binding


Well, on the basis of the above knowledge, let us look at the beginning>/dev/null 2>&1. This command is actually divided into two commands, one is>/dev/null, the other is2>&1.


1. >/dev/null


The purpose of this command is to redirect the standard output 1 to/dev/null. /dev/null represents an empty device file for Linux, and all content written to this file will be lost, commonly known as "black holes". Then>/dev/null, when executed, the standard output will no longer exist, and there is no place to find the content of the output.


2.2>&1


This command uses the redirect binding, with & to bind two outputs together. The function of this command is that the error output will be in the same file descriptor as the standard output, saying that the error output will be output to the same place as the standard output.



Before executing the shell command, Linux determines all the input and output locations and executes the redirected command from left to right, so that>/dev/null 2>&1the standard output is redirected to/dev/null (discarding the standard output), and then the error output is due to the reuse of the standard output descriptor. So the error output is also directed to the/dev/null, and the error output is also discarded. After executing this command, the shell command will not output any information to the console, and no information will be output to the file.


>/dev/null 2>&1 VS 2>&1 >/dev/null


Back to the beginning of the article, I said I had reversed>/dev/nulland2>&1assembled the order, resulting in a small problem. At first glance, these two orders seem to be equivalent, but in fact they are very different. As mentioned earlier, Linux will determine all the input and output locations before executing the shell command, and execute the redirected commands from left to right. Then we also analyze from left to right2>&1 >/dev/null:


    1. 2>&1, bind the error output to the standard output. Because the standard output at this point is the default value, which is output to the screen, the error output is output to the screen.
    2. >/dev/null, redirect standard output 1 to/dev/null.


We use a table to better illustrate the differences between the two commands:


Command Standard Output Error Output
>/dev/null 2>&1 Discarded Discarded
2>&1 >/dev/null Discarded Screen
>/dev/null 2>&1 VS >/dev/null 2>/dev/null


Then there may be some students wondering why you should use redirect binding instead of repeating it like>/dev/null 2>/dev/nullthis.



To answer this question, let's go back to the scenario where we just introduced the output redirection. We tried to direct the standard output and error output to the out file:


1234 # ls a.txt b.txt >out 2>out# cat outa.txt? Method Access B.txt: No file or directory


WTF? Even if there is garbled, this is why? This is because the standard output and error output will preempt the pipeline to the out file, so it may cause missing, overwritten, and so on when the content is output. Now there is garbled, sometimes there may be only error messages or only normal information situation. In any case, the final situation cannot be estimated by using this notation.



Moreover, because the out file was opened two times, two file descriptors will be preempted to the file output content, so the overall IO efficiency is not as>/dev/null 2>&1high.


Nohup Combination


We often usenohup command &command form to launch some background programs, such as some Java services:


1 # nohup Java-jar Xxxx.jar &


To keep some execution information out of the foreground (console), we'll add the command we just mentioned>/dev/null 2>&1to discard all the output:


1 # nohup Java-jar Xxxx.jar >/dev/null 2>&1 &
Summarize


This article mainly introduces the principle of Linux redirection and some basic commands, and analyzes>/dev/null 2>&1this command and some points of attention in detail.



All in all, the most used in the work is thenohup command >/dev/null 2>&1 &order, I hope we can master it well.


Resources
    1. Linux Redirection Summary
    2. The difference between >/dev/null 2>&1 and 2>&1 >/dev/null


>/dev/null 2>&1 in the shell


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.