博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1
阅读量:4327 次
发布时间:2019-06-06

本文共 2092 字,大约阅读时间需要 6 分钟。

 

Reference link: http://unix.stackexchange.com/questions/70963/difference-between-2-2-dev-null-dev-null-and-dev-null-21

 

 
3
5

Just looking for the difference between

  • 2>&-
  • 2>/dev/null
  • |&
  • &>/dev/null
  • >/dev/null 2>&1

and their portability with non-Bourne shells like tcshmksh, etc.

 
    
Note that, while mksh supports &> for GNU bash compatibility, it’s strongly encouraged to not use this, as parsing it can break the semantics of existing POSIX scripts, and mksh disables that in POSIX mode already. –    
add comment

2 Answers

6
accepted

For background:

  • number 1 = standard out (i.e. STDOUT)
  • number 2 = standard error (i.e. STDERR)
  • if a number isn't explicitly given, then number 1 is assumed by the shell (bash)

First let's tackle the function of these. For reference see the .

Functions

2>&-

The general form of this one is M>&-, where "M" is a file descriptor number. This will close output for whichever file descriptor is referenced, i.e. "M".

2>/dev/null

The general form of this one is M>/dev/null, where "M" is a file descriptor number. This will redirect the file descriptor, "M", to /dev/null.

2>&1

The general form of this one is M>&N, where "M" & "N" are file descriptor numbers. It combines the output of file descriptors "M" and "N" into a single stream.

|&

This is just an abbreviation for 2>&1. It was added in Bash 4.

&>/dev/null

This is just an abbreviation for 2>&1 >/dev/null. It too was added in Bash 4. It redirects file descriptor 2 (STDERR) and descriptor 1 (STDOUT) to /dev/null.

>/dev/null

This is just an abbreviation for 1>/dev/null. It redirects file descriptor 1 (STDOUT) to /dev/null.

Portability to non-bash, tcsh, mksh, etc.

I've not dealt much with other shells outside of csh and tcsh. My experience with those 2 compared to bash's redirection operators, is that bash is superior in that regard. See the  for more details.

Of the commands you asked about none are directly supported by csh/tcsh. You'd have to use different syntaxes to construct similar functions.

转载于:https://www.cnblogs.com/rosepotato/p/3709315.html

你可能感兴趣的文章
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>