


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)
What do 1 and 2 represent in Linux?
In Linux system 0 1 2 is a file descriptor
##As can be seen from the above table, theecho "hello" > t.log
echo "hello" 1> t.log
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 &
(最后一个&表示把条命令放到后台执行,不是本文重点,不懂的可以自行Google)
为什么2>&1一定要写到>log后面,才表示标准错误输出和标准输出都定向到log中?
我们不妨把1和2都理解是一个指针,然后来看上面的语句就是这样的:
本来1----->屏幕 (1指向屏幕) 执行>log后, 1----->log (1指向log) 执行2>&1后, 2----->1 (2指向1,而1指向log,因此2也指向了log) `` 再来分析下
nohup java -jar app.jar 2>&1 >log &
本来1----->屏幕 (1指向屏幕) 执行2>&1后, 2----->1 (2指向1,而1指向屏幕,因此2也指向了屏幕) 执行>log后, 1----->log (1指向log,2还是指向屏幕)
所以这就不是我们想要的结果。
搜索公众号GitHub猿后台回复“打飞机”,获取一份惊喜礼包。
简单做个试验测试下上面的想法:
java代码如下:
public class Htest { public static void main(String[] args) { System.out.println("out1"); System.err.println("error1"); } }
javac编译后运行下面指令:
java Htest 2>&1 > log
你会在终端上看到只输出了"error1",log文件中则只有"out1"
每次都写">log 2>&1"太麻烦,能简写吗?
有以下两种简写方式
&>log >&log
比如上面小节中的写法就可以简写为:
nohup java -jar app.jar &>log &
上面两种方式都和">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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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

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

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

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

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

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.

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
