&>
and >&
semi-equivalence (clobber)
The zsh
manual Redirections section says that:
are equivalent.
Both will clobber the file - truncate it file to 0 bytes before writing to it, just like > file
would do in the STDIN-only case.
However, the bash
manual Redirections section adds that:
Of the two forms, the first is preferred. This is semantically equivalent to
>word 2>&1
When using the second form, word may not expand to a number or -
. If it does, other redirection operators apply (see Duplicating File Descriptors below) for compatibility reasons.
So, whilst you tagged zsh
, it's probably good practice to get finger memory in the first form should one ever write a bash
script.
>> logfile 2>&1
and &>>
equivalence (append)
Here, logfile
is not overwritten, but opened for writing at the end of the file, ie append mode (O_APPEND
).
The equivalent in both {ba,z}sh
is:
command1 &>> logfile
In bash
:
The format for appending standard output and standard error is:
&>>word
This is semantically equivalent to
>>word 2>&1
(see Duplicating File Descriptors below).
(Note: the clobber usage of &>
over >&
in the section above is again recommended given there is only one way for appending in bash
.)
zsh
allows both &>>
and >>&
forms.