Deeply understand the meaning of 2 > & 1 in Linux shell (the most complete in the whole network, you can understand it after reading it)

Time:2021-8-30

What do 1 and 2 stand for in Linux

On Linux systems, 0 1 2 is a file descriptor

Deeply understand the meaning of 2 > & 1 in Linux shell (the most complete in the whole network, you can understand it after reading it)

As can be seen from the above table, we usually use

echo "hello" > t.log 

In fact, it can also be written as

echo "hello" 1> t.log

About the meaning of 2 > & 1

I / O redirection will not be discussed in detail in this article. For those who do not understand, please refer to here:Shell: pipeline and redirection

  • Meaning: redirect standard error output to standard output
  • The symbol > & is a whole and cannot be separated. After separation, it will not have the above meaning.

    • For example, some people may think that 2 is standard error input, 1 is standard output, and > is a redirection symbol. Should “redirect standard error output to standard output” be written as “2 > 1”? Is that so?
    • If you have tried, you will know that the writing method of 2 > 1 actually redirects the standard error output to a file named “1”
  • It is also not allowed to write 2 & > 1

Why 2 > & 1 should be put in the back

Consider the following shell command

nohup java -jar app.jar >log 2>&1 &

(the last & means that the command is executed in the background, which is not the focus of this article. Those who do not understand can Google by themselves)

Why 2 > & 1 must be written after > log to indicate that both standard error output and standard output are directed to log?

We might as well understand 1 and 2 as a pointer, and then look at the above statement as follows:

1 ----- > screen (1 points to the screen)
After executing > log, 1 ----- > log (1 points to log)
After executing 2 > & 1, 2 ----- > 1 (2 points to 1 and 1 points to log, so 2 also points to log)
``
Let's analyze it again

nohup java -jar app.jar 2>&1 >log &

1 ----- > screen (1 points to the screen)
After executing 2 > & 1, 2 ----- > 1 (2 points to 1 and 1 points to the screen, so 2 also points to the screen)
After executing > log, 1 ----- > log (1 points to log, 2 points to the screen)

So that’s not what we want.

Simply do an experiment to test the above idea:

The Java code is as follows:

public class Htest {
    public static void main(String[] args) {
        System.out.println("out1");
        System.err.println("error1");
    }
}

Run the following instructions after javac compilation:

java Htest 2>&1 > log

You will see that only “error1” is output on the terminal, and only “out1” is output in the log file

It’s too troublesome to write “> log 2 > & 1” every time. Can you abbreviate it?

There are two abbreviations

&>log
>&log

For example, the wording in the above section can be abbreviated as:

nohup java -jar app.jar &>log &

Both of the above two methods have the same semantics as “> log 2 > & 1”.

Is there any difference between & > and > & in the above two methods?

There is no semantic difference, but the first method is the best choice. Generally, the first method is used.

Author: a walking people
Original text:https://blog.csdn.net/zhaomin…

Deeply understand the meaning of 2 > & 1 in Linux shell (the most complete in the whole network, you can understand it after reading it)

Recommended Today

Monkey patch monkey patch programming method and its application in Ruby

What ismonkey patch (Monkey Patch)? In a dynamic language, functions are added and changed without modifying the source code. Purpose of using monkey patch:1. Additional function2. Function change3. Fix program errors4. Add a hook to execute some other processing while executing a method, such as printing logs, realizing AOP, etc,5. Cache, when the amount of […]