Federico Zabaleta
Federico Zabaleta

Reputation: 33

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&

Upvotes: 3

Views: 1125

Answers (1)

ruakh
ruakh

Reputation: 183351

According to The Bash Reference Manual, §3.6.4 "Redirecting Standard Output and Standard Error", the >& notation redirects standard output and standard error together.

In your specific example, I wouldn't expect echo "Hello world" to write anything to standard error, so the & doesn't really change anything; but if it did write things to both standard output and standard error, then log would end up with both.

Incidentally, per that same section, the equivalent notation &> is preferred over >& (presumably because >& is liable to be confused with notations like >&1, which does something quite different).

Upvotes: 4

Related Questions