A redirection operator changes where output is going (or where input is coming from). 3>&1
means “make file descriptor 3 point wherever file descriptor 1 is currently pointing” (which is the terminal). 3>file
means “make file descriptor 3 point to file
”. Nothing happened during the brief time fd 3 was pointing to the terminal, so you don't get any terminal output.
In order to obtain the same data in two locations, something needs to copy the data. This is the job of tee
. For every byte that it reads, it outputs that byte twice (if given one file argument, plus its standard output).
Don't get bogged down by the fact that >&
is sometimes called duplication. What it's duplicating is the file descriptor: 3>&1
duplicates fd 1 to fd 3, meaning that the data going to fd 1 and the data going to fd 3 are getting merged — they're both going to wherever fd 1 was pointing.
If you prefer graphical explanations, see what is meant by connecting STDOUT and STDIN? and How can a command have more than one output?
In any case, your command doesn't output anything on file descriptor 3, so redirecting fd 3 doesn't change anything. The date
command writes to its standard output, i.e. fd 1, and you aren't redirecting that.
Zsh has a feature called multios that changes the meaning of output redirection. If there are multiple output redirections for the same command on the same file descriptor, then the first one changes where that descriptor is pointing, but subsequent ones replicate the data to the specified targets. For example, to get output in a file in addition to wherever stdout was pointing, you can use
date >&1 >file
Zsh is doing the job of tee
. Note that the order of redirections matter — date >file >&1
would write to file
twice, since by the time the >&1
operator is evaluated the standard output of date
is already going to file
.