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

 

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.

asked Apr 3 '13 at 1:25
Det
12315
 
    
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. –  mirabilos Feb 27 at 13:56

add comment

2 Answers

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 Advanced Bash-Scripting Guide.

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 tcsh man page 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.

Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1的更多相关文章

  1. 为什么获取的System.Web.HttpContext.Current值为null,HttpContext对象为null时如何获取程序(站点)的根目录

    ASP.NET提供了静态属性System.Web.HttpContext.Current,因此获取HttpContext对象就非常方便了.也正是因为这个原因,所以我们经常能见到直接访问System.W ...

  2. JS中NULL和undifined区别及NULL的作用

    1.博客地址:http://www.cnblogs.com/eastday/archive/2010/03/03/1677324.html 2.参考地址2:https://www.zhihu.com/ ...

  3. 关于 JavaScript 的 null 和 undefined,判断 null 的真实类型

    null.undefined 博客地址: https://ainyi.com/39 undefined:表示一个变量最原始的状态,而非人为操作的结果 null:表示一个对象被人为的重置为空对象,而非一 ...

  4. eclipse 中运行 Hadoop2.7.3 map reduce程序 出现错误(null) entry in command string: null chmod 0700

    运行map reduce任务报错: (null) entry in command string: null chmod 0700 解决办法: 在https://download.csdn.net/d ...

  5. isset、empty、var==null、is_null、var===null详细理解

    //isset: 判断变量是否被初始化 //它并不会判断变量是否为空,并且可能用来判断数组中元素是否被定义 //听说在数组用isset与array_key_exists高出4倍 $a = " ...

  6. AndroidStudio编译错误:Error: null value in entry: blameLogFolder=null

    今天写项目的时候,电脑开了个WiFi热点,然后这个热点和window驱动不兼容,有时候会导致电脑重启,重启之后AndroidStudio编译就报错了, Error: null value in ent ...

  7. 修改ActiveReports验证文字“给不能为 null 的参数指定一个 null 值”

    转:http://gcdn.gcpowertools.com.cn/showtopic-13759.html ActiveReports官方网站:http://www.gcpowertools.com ...

  8. 对”唯一键可以包含NULL值,并且每个NULL值都是唯一的(即NULL!=NULL)“理解

    因为最近在写一篇关于字符串模糊检索的论文,开始比较细致的研究数据库(MySQL)中的index问题,变到图书馆借了本<Effective MySQL之SQL语句最优化>(Ronald Br ...

  9. sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) Cannot add a NOT NULL column with default value NULL [SQL: u'ALTER TABLE address_scopes ADD COLUMN ip_version INTEGER NOT NULL']

    root@hett-virtual-machine:~# su -s /bin/sh -c "neutron-db-manage --config-file /etc/neutron/neu ...

  10. c语言NULL和0区别及NULL详解

      先看下面一段代码输出什么: #include<stdo.h> int main() { int *p=NULL; printf("%s",p); } 输出<n ...

随机推荐

  1. NET

    NET狂官方面试题-数据库篇答案   题目:http://www.cnblogs.com/dunitian/p/6028838.html 汇总:http://www.cnblogs.com/dunit ...

  2. C# Double toString保留小数点方法

    有时候double型数据需要toString(),但又想保留小数,当值为整数,比如3.00时tostring后会变为”3″,具体说明见下: 1 string str0 = i.ToString(&qu ...

  3. DB天气app冲刺二阶段第五天

    昨天什么事情也没做..看了一场哆啦a梦 所以就不算冲刺了.. 今天主要就是做了一下需要用到的图片的整理还有的就是UI主界面需要展示用的素材,发现好多东西都需要搜索半天,虽然这个不是什么技术活..但真的 ...

  4. mysql存储过程中传decimal值会自动四舍五入,没有小数

    通过 call  proc(0.2,0.5);  查看结果数据库竟然是0  和 1 原因:proc的参数没有设置好 参数:原本是  in a decimal,in b decimal 应该改为:in ...

  5. php 安全处理方案

    Safe::mysqlSafe(); sql注入,升级5.3.6以上版本php 方案一:将所有请求中所有数据(get/post/cookie)实现mysql_escape_string进行安全处理. ...

  6. Eclipse中的Web项目自动部署到Tomcat(转)

    转自:http://www.cnblogs.com/ywl925/p/3815173.html 问题: 这里就有个问题,是怎么把Eclipse中的网站项目自动部署到tomcat中.在Eclipse中做 ...

  7. C# 数据结构--排序[上]

    概述 看了几天的排序内容,现在和大家分享一些常见的排序方法. 啥是排序? 个人理解的排序:通过对数组中的值进行对比,交换位置最终得到一个有序的数组.排序分为内存排序和外部排序.本次分享排序方法都为内存 ...

  8. 数字PID控制算法

    增量式PID控制算法 量式PID控制算法 2009-07-18 10:33 (转载 出处blog.ednchina.com/tengjingshu )blog.ednchina.com/tengjin ...

  9. 【redis】06Redis的高级应用之事务处理、持久化操作、pub_sub、虚拟内存

    上节课详细讲解了redis数据库的常用命令,以及redis数据库高级应用当中的, 安全性,跟咱们的主从复制, 这节课呢,咱们继续来讲咱们的高级应用, 首先来看一下咱们的事务处理, 事务处理 我前面说过 ...

  10. Lua print on the same line

    In Pascal, I have write and writeln. Apparently Lua's print is similar to writeln of Pascal. Do we h ...