Chinaunix首页 | 论坛 | 博客
  • 博客访问: 670594
  • 博文数量: 207
  • 博客积分: 1743
  • 博客等级: 上尉
  • 技术积分: 2044
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-20 14:36
文章分类

全部博文(207)

文章存档

2016年(24)

2015年(10)

2014年(50)

2013年(45)

2012年(78)

分类: LINUX

2014-03-27 17:47:24

ash环境作了如下实验:

[root@s253.ipcpu.com ~]# balabala
-bash: balabala: command not found
[root@s253.ipcpu.com ~]# balabala &> /dev/null
[root@s253.ipcpu.com ~]# balabala >/dev/null 2>&1
[root@s253.ipcpu.com ~]# balabala > /dev/null 2>&1
[root@s253.ipcpu.com ~]#

两条命令等价。

网友给出ksh和bash对比
/home/lee#echo $0
-ksh
/home/lee#ls abc >/dev/null 2>&1
/home/lee#ls abc &>/dev/null
ls: abc: 没有那个文件或目录
[1] 1524
[1] + Done (1)             ls –color=tty abc
/home/lee#bash
/home/lee#echo $0
bash
/home/lee#ls abc >/dev/null 2>&1
/home/lee#ls abc &>/dev/null
显然在ksh就不一样了,所以推荐使用>/dev/null 2>&1

参考资料:

后记:

2>&1 和 &>  的解释

Linux的IO输入输出有三类

Standard Input      代码 0
Standard Output  代码  1
Standard Error     代码 2

ls thereisno 1> out.txt  标准输出重定向  也可以不加1写成 ls thereisno > out.txt

 ls thereisno 2> out.txt  标准错误重定向

 ls thereisno > out.txt  2>&1  把错误定向到标准输出,然后统一重定向

 ls thereisno &> out.txt  所有IO重定向(&>中的&可以

阅读(746) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~