2

What is the difference between this redirection

some-program &> some_file

and this one?

some-program > some_file 2>&1
Kusalananda
  • 333,661

2 Answers2

7

In the bash and zsh shells, there is no difference between the two. The &>file redirection is syntactic sugar implemented as an extension to the POSIX standard that means exactly the same thing as the standard >file 2>&1.

Note that using the &> redirection in a script executed by a non-bash/zsh interpreter will likely break your script in interesting ways, as & and > would be interpreted independently of each other.

some_command &>file

would, in a non-bash/zsh script, be the same as

some_command & >file

and as

some_command &
>file

This starts some_command as a background job, and truncates/creates the file called file.

Also related:

Kusalananda
  • 333,661
4

The second form is POSIX-compliant, and will work in any POSIX shell.

The first form is exactly equivalent, but it will only work in shells that support the shortened form (Bash and Zsh, to name two), and will fail in others (Dash, for example, the /bin/sh implementation used in Debian and derivatives).

If portability is important, you should use the two-step form.

Stephen Kitt
  • 434,908