Table of Contents
What do 1 and 2 represent in Linux? " >What do 1 and 2 represent in Linux?
" >About 2> The meaning of &1
为什么2>&1要放在后面" >为什么2>&1要放在后面
每次都写">log 2>&1"太麻烦,能简写吗?" >每次都写">log 2>&1"太麻烦,能简写吗?
Home Operation and Maintenance Linux Operation and Maintenance In-depth understanding of the meaning of 2>&1 in Linux shell (the most comprehensive on the entire network, you will understand after reading it)

In-depth understanding of the meaning of 2>&1 in Linux shell (the most comprehensive on the entire network, you will understand after reading it)

Aug 03, 2023 pm 04:14 PM
linux shell


What do 1 and 2 represent in Linux?

In Linux system 0 1 2 is a file descriptor

In-depth understanding of the meaning of 2>&1 in Linux shell (the most comprehensive on the entire network, you will understand after reading it)
##As can be seen from the above table, the

echo "hello" > t.log
Copy after login

we usually use can actually be written as

echo "hello" 1> t.log
Copy after login

About 2> The meaning of &1

I won’t go into details about input/output redirection in this article

  • 含义:将标准错误输出重定向到标准输出
  • 符号>&是一个整体,不可分开,分开后就不是上述含义了。
    • 比如有些人可能会这么想:2是标准错误输入,1是标准输出,>是重定向符号,那么"将标准错误输出重定向到标准输出"是不是就应该写成"2>1"就行了?是这样吗?
    • 如果是尝试过,你就知道2>1的写法其实是将标准错误输出重定向到名为"1"的文件里去了
  • 写成2&>1也是不可以的

为什么2>&1要放在后面

考虑如下一条shell命令

nohup java -jar app.jar >log 2>&1 &
Copy after login

(最后一个&表示把条命令放到后台执行,不是本文重点,不懂的可以自行Google)

为什么2>&1一定要写到>log后面,才表示标准错误输出和标准输出都定向到log中?

我们不妨把1和2都理解是一个指针,然后来看上面的语句就是这样的:

本来1----->屏幕 (1指向屏幕)
执行>log后, 1----->log (1指向log)
执行2>&1后, 2----->1 (2指向1,而1指向log,因此2也指向了log)
``
再来分析下
Copy after login

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

本来1----->屏幕 (1指向屏幕)
执行2>&1后, 2----->1 (2指向1,而1指向屏幕,因此2也指向了屏幕)
执行>log后, 1----->log (1指向log,2还是指向屏幕)
Copy after login

所以这就不是我们想要的结果。

搜索公众号GitHub猿后台回复“打飞机”,获取一份惊喜礼包。

简单做个试验测试下上面的想法:

java代码如下:

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

javac编译后运行下面指令:

java Htest 2>&1 > log
Copy after login

你会在终端上看到只输出了"error1",log文件中则只有"out1"

每次都写">log 2>&1"太麻烦,能简写吗?

有以下两种简写方式

&>log
>&log
Copy after login

比如上面小节中的写法就可以简写为:

nohup java -jar app.jar &>log &
Copy after login

上面两种方式都和">log 2>&1"一个语义。

那么 上面两种方式中&>和>&有区别吗?

语义上是没有任何区别的,但是第一中方式是最佳选择,一般使用第一种。

The above is the detailed content of In-depth understanding of the meaning of 2>&1 in Linux shell (the most comprehensive on the entire network, you will understand after reading it). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I use regular expressions (regex) in Linux for pattern matching? How do I use regular expressions (regex) in Linux for pattern matching? Mar 17, 2025 pm 05:25 PM

The article explains how to use regular expressions (regex) in Linux for pattern matching, file searching, and text manipulation, detailing syntax, commands, and tools like grep, sed, and awk.

How do I monitor system performance in Linux using tools like top, htop, and vmstat? How do I monitor system performance in Linux using tools like top, htop, and vmstat? Mar 17, 2025 pm 05:28 PM

The article discusses using top, htop, and vmstat for monitoring Linux system performance, detailing their unique features and customization options for effective system management.

How do I implement two-factor authentication (2FA) for SSH in Linux? How do I implement two-factor authentication (2FA) for SSH in Linux? Mar 17, 2025 pm 05:31 PM

The article provides a guide on setting up two-factor authentication (2FA) for SSH on Linux using Google Authenticator, detailing installation, configuration, and troubleshooting steps. It highlights the security benefits of 2FA, such as enhanced sec

How do I configure SELinux or AppArmor to enhance security in Linux? How do I configure SELinux or AppArmor to enhance security in Linux? Mar 12, 2025 pm 06:59 PM

This article compares SELinux and AppArmor, Linux kernel security modules providing mandatory access control. It details their configuration, highlighting the differences in approach (policy-based vs. profile-based) and potential performance impacts

How do I back up and restore a Linux system? How do I back up and restore a Linux system? Mar 12, 2025 pm 07:01 PM

This article details Linux system backup and restoration methods. It compares full system image backups with incremental backups, discusses optimal backup strategies (regularity, multiple locations, versioning, testing, security, rotation), and da

How do I use sudo to grant elevated privileges to users in Linux? How do I use sudo to grant elevated privileges to users in Linux? Mar 17, 2025 pm 05:32 PM

The article explains how to manage sudo privileges in Linux, including granting, revoking, and best practices for security. Key focus is on editing /etc/sudoers safely and limiting access.Character count: 159

How do I manage software packages in Linux using package managers (apt, yum, dnf)? How do I manage software packages in Linux using package managers (apt, yum, dnf)? Mar 17, 2025 pm 05:26 PM

Article discusses managing software packages in Linux using apt, yum, and dnf, covering installation, updates, and removals. It compares their functionalities and suitability for different distributions.

How do I set up a firewall in Linux using firewalld or iptables? How do I set up a firewall in Linux using firewalld or iptables? Mar 12, 2025 pm 06:58 PM

This article compares Linux firewall configuration using firewalld and iptables. Firewalld offers a user-friendly interface for managing zones and services, while iptables provides low-level control via command-line manipulation of the netfilter fra

See all articles