Deeply understand the meaning of 2 > & 1 in Linux shell

Time:2021-7-19

What do 1 and 2 stand for in Linux

In Linux system, 0 1 2 is a file descriptor

Deeply understand the meaning of 2 > & 1 in Linux shell

It can be seen from the above table that we usually use

echo "hello" > t.log 

In fact, it can also be written as

echo "hello" 1> t.log

On the meaning of 2 > & 1

This article will not elaborate on I / O redirection. For those who do not understand it, please refer to here:Shell: pipeline and redirection

  • Meaning: redirecting standard error output to standard output
  • Symbol > & is a whole, can not be separated, separated is not the above meaning.

    • For example, some people may think like this: 2 is standard error input, 1 is standard output, > is redirection symbol, so “redirecting standard error output to standard output” should be written as “2 > 1”? Is that so?
    • If you have tried, you will know that 2 > 1 is actually redirecting the standard error output to a file named “1”
  • It is not allowed to write 2 & > 1

Why should 2 > & 1 be put behind

Consider the following shell command

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

(the last one means to put the command to the background for execution, which is not the focus of this article. If you don’t understand it, you can google it yourself.)

Why must 2 > & 1 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 sentence

Original 1: - > screen (1 points to 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 &

Original 1: - > screen (1 points to 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 screen)

So that’s not what we want.

Do a simple 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");
    }
}

After javac is compiled, run the following instructions:

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 above section can be abbreviated as:

nohup java -jar app.jar &>log &

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

Is there any difference between the above two ways?

Semantically, there is no difference, but the first way is the best choice. Generally, the first way is used.

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

Deeply understand the meaning of 2 > & 1 in Linux shell

Recommended Today

Programming Xiaobai must understand the network principle

How is the network composed? Why can we easily surf the Internet now?Whether you are a computer major or not, you may always have such questions in your heart!And today we will solve this matter and tell you the real answer! Basic composition of network First, let’s look at this sentence Connect all computers together […]