shell中可能常常能看到:>/dev/null 2>&1
命令的結果能夠經過%>的形式來定義輸出
/dev/null 表明空設備文件
> 表明重定向到哪裏,例如:echo "123" > /home/123.txt
1 表示stdout標準輸出,系統默認值是1,因此">/dev/null"等同於"1>/dev/null"
2 表示stderr標準錯誤
& 表示等同於的意思,2>&1,表示2的輸出重定向等同於1
1 表示stdout標準輸出,系統默認值是1,因此">/dev/null"等同於 "1>/dev/null"
&>/dev/null 等價於 >/dev/null 2>&1
那麼本文標題的語句:
1>/dev/null 首先表示標準輸出重定向到空設備文件,也就是不輸出任何信息到終端,說白了就是不顯示任何信息。
2>&1 接着,標準錯誤輸出重定向等同於標準輸出,由於以前標準輸出已經重定向到了空設備文件,因此標準錯誤輸出也重定向到空設備文件。
--------------------------------------------------------------
在一些Shell腳本中,特別是Crontab的腳本中,常常會看到 >/dev/null 2>&1這樣的寫法。
其實這個很好理解。咱們分兩部分解釋。
1. >/dev/null
你們知 「>」(右尖括號) 在unix/linux shell 中表示 輸入到 的意思,就是把」>」左邊的內容輸入到」>」右邊。
好比:echo text>1.txt 就把「text」這個文本輸入到1.txt這個文件中。
那麼 「/dev/null」 又是個什麼東東呢?它表明一個空設備,即不存在的設備。也就是說,拋棄」>」左邊的內容,不進行輸出。
2. 2>&1
這個實際上是三個部分組成的:2, >&, 1 。咱們先來搞清楚這裏的2和1是什麼意思。在/usr/include/unistd.h中,你能夠找到以下代碼。
/* Standard file descriptors. */
#define STDIN_FILENO 0 /* Standard input. */
#define STDOUT_FILENO 1 /* Standard output. */
#define STDERR_FILENO 2 /* Standard error output. */
這是三種不一樣的流。
2表明stderr.
1表明sdtout.
而 &> 則表示把符號左邊的內容以符號右邊的形式輸出。
2&>1 就是把,把stderr作爲stdout輸出。
如今咱們結合這兩個部分來看。2&>1定義了把stderr作爲標準的stdout流輸出,而後stdout的內容所有寫入/dev/null,也就是說被捨棄掉。
結論就是,不管執行的是什麼命令,哪怕運行中出現了error都不會有回顯。
-----------------------------------------------------------------
爲何要用 /dev/null 2>&1 這樣的寫法.
這條命令的意思是將標準輸出和錯誤輸出所有重定向到/dev/null中,也就是將產生的全部信息丟棄.下面我就爲你們來講一下, command > file 2>file 與command > file 2>&1 有什麼不一樣的地方.
首先~command > file 2>file 的意思是將命令所產生的標準輸出信息,和錯誤的輸出信息送到file 中.command > file 2>file 這樣的寫法,stdout和stderr都直接送到file中, file會被打開兩次,這樣stdout和stderr會互相覆蓋,這樣寫至關使用了FD1和FD2兩個同時去搶佔file 的管道.
而command >file 2>&1 這條命令就將stdout直接送向file, stderr 繼承了FD1管道後,再被送往file,此時,file 只被打開了一次,也只使用了一個管道FD1,它包括了stdout和stderr的內容.
從IO效率上,前一條命令的效率要比後面一條的命令效率要低,因此在編寫shell腳本的時候,較多的時候咱們會用command > file 2>&1 這樣的寫法.
----------------------------------------------------------------
> /dev/null 2>&1
You need to understand the theory first and then its upto you how and where you want to apply that theory. I'll try to explain above to you.
The greater-than (>) in commands like these redirect the program’s output somewhere. In this case, something is being redirected into /dev/null, and something is being redirected into &1.
Standard in, out and error:
There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as 「data pipes」) are often called STDIN, STDOUT, and STDERR.
Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.
That means file descriptor 0 or fd0 denotes STDIN or standard input and file descriptor 1 or fd1 denotes STDOUT or standard output and file descriptor 2 or fd2 denotes STDERR or standard error.
You can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).
The short explanation, therefore, is 「all output from this command should be shoved into a black hole.」 That’s one good way to make a program be really quiet!