2

What is the difference between this redirection

some-program &> some_file

and this one?

some-program > some_file 2>&1
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:

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.