首页 > 解决方案 > Difference between ">& log" and ">& log&"

问题描述

I don't get what is the difference between all these commands in bash,

echo "Hello world" > log&
echo "Hello world" >& log
echo "Hello world" >& log&

What does each & do?

As far as I understand putting & at the end send the whole thing to the background. But what about the & after the > ?

Is there any advantage of writing

echo "Hello world" >& log&

instead of

echo "Hello world" > log&

标签: bashio-redirection

解决方案


根据The Bash Reference Manual , §3.6.4 "Redirecting Standard Output and Standard Error",该>&符号将标准输出和标准错误一起重定向。

在您的具体示例中,我不希望echo "Hello world"向标准错误写入任何内容,因此&不会真正改变任何内容;但如果它确实将东西写入标准输出和标准错误,那么log最终会同时出现。

顺便说一句,在同一节中,等价的符号&>优于>&(大概是因为>&容易与类似的符号混淆>&1,后者的作用完全不同)。


推荐阅读