shell to redirect input

A.> / Dev / null

This command action is a standard output is redirected to / dev / null in.
/ dev / null null device on behalf of the linux file, all the contents of this file to be written inside will be lost, commonly known as "black hole." After you perform a> / dev / null, standard output will no longer exist, there is no place to find the output.

二.2>&1

And standard error output will be output to the same place, linux before executing shell commands, will determine a good location for all input and output, and redirect commands executed from left to right, so> / dev / null
2> & action 1 is to make the standard output is redirected to / dev / null (for discarding the standard output), then the error output due to the reuse of the standard output descriptor, the error output is also directed to / dev / null, the error output is also He discarded. After executing this command, shell command that section will not output any information to the console, there will not be any information to the output file.

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

At first glance looks like these two commands are equivalent, but in fact very different. Just mentioned, linux before executing shell commands, will determine a good location for all input and output, and from left to right to perform redirection command. So we have to analyze the same from left to right 2> & 1> / dev / null: 2> & 1, the error output is bound to the standard output. Because at this time it is the default standard output, which is output to the screen, so the error output will be output to the screen. dev / null, the standard output is redirected to / dev / null in. .

We use a table to compare the difference between the two:

We use a form to better illustrate the difference between these two commands:

command Standard output Error output
>/dev/null 2>&1 throw away throw away
2>&1 >/dev/null throw away screen
发布了25 篇原创文章 · 获赞 0 · 访问量 410

Guess you like

Origin blog.csdn.net/m0_38028438/article/details/104611336