It's swapping stdout
and stderr
.
>name
means redirect output to file name
.
>&number
means redirect output to file descriptor number
.
So the &
is needed to tell the shell you mean a file descriptor, not a file name.
A file descriptor is a number that refers to an already open file. The standard ones are 0
for standard input, 1
for standard output or 2
for standard error. You can also use any other number, which will create a new file descriptor, just like when you create a new variable with var=value
.
By default, both file descriptor 1
and 2
go to /dev/tty
, so if you run somecommand 3>&1 1>&2 2>&3
in a new shell, it doesn't change anything (except now you have a file descriptor number 3).
But if somewhere earlier in the script it does a redirection using exec (e.g. exec 2>error.log
), or the script is run with a command line including redirection (e.g. ./thescript 2>error.log
), then swapping stdout and stderr will do something.
In your specific case, the command that's having its stdout and stderr swapped is dialog
. Looking at its man page, I see
Some widgets, e.g., checklist, will write text to dialog's output.
Normally that is the standard error
so perhaps the person who wrote the script wants dialog
's output to go to stdout
instead of stderr
for some reason.
See also Order of redirections