bash - "&> foo"和 "> foo 2>&1"有什么区别?

标签 bash

似乎有两个 bash 习语用于将 STDOUT 和 STDERR 重定向到文件:

fooscript &> foo

...和...

fooscript > foo 2>&1

有什么区别?在我看来,第一个只是第二个的快捷方式,但我的同事争辩说,即使初始重定向出现错误,第二个也不会产生任何输出,而第一个会将重定向错误吐到 STDOUT。

编辑:好的...似乎人们不明白我在问什么,所以我会尝试澄清:

谁能给我一个例子,上面写的两行特定行会产生不同的行为?

最佳答案

来自bash manual :

There are two formats for redirecting standard output and standard error:

&>word

and

>&word

Of the two forms, the first is preferred. This is semantically equivalent to

 >word 2>&1

短语“语义等价”应该可以解决您与同事的问题。

关于bash - "&> foo"和 "> foo 2>&1"有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/188208/

相关文章:

bash:while 循环内的计数器(kill 和 kill -9)

linux - 递归检查shell中文件之间的差异

bash - 如何查看所有(包括隐藏)子目录

bash - 将视频从 60 FPS 转换为 25 FPS 而不会减慢它

bash - 将输出保存到变量中

linux - sleep 过程直到在 Go 中完成

bash - bash vi shell 模式下的 Tab 自动补全

linux - 用于终止早于 x 时间的进程的 Bash 脚本 - 表达式中的语法错误

bash - 修改文件中一行的一部分

linux - 测试目录是否有特定名称