Unix & Linux: What does "3＞&1 1＞&2 2＞&3" do in a script? (3 Solutions!!)
Channel: Roel Van de Paar
Date: 2020-08-14

Unix & Linux: What does "3＞&1 1＞&2 2＞&3" do in a script?


The Question: I saw this line in a script:
DEVICE=`dialog --inputbox "Festplatten-Laufzeit auslesen. Gebe Sie das
gewunschte Device an: " 0 70 "" 3＞&1 1＞&2 2＞&3`
What is
3＞&1 1＞&2 2＞&3
doing? I know that 1 = stdout and 2 = stderr, but what are the 3 and the & for?

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 82 people ==
The numbers are file descriptors and only the first three (starting with zero)
have a standardized meaning:
0 - stdin
1 - stdout
2 - stderr
So each of these numbers in your command refer to a file descriptor. You can
either redirect a file descriptor to a file with ＞ or redirect it to another
file descriptor with ＞&
The 3＞&1 in your command line will create a new file descriptor and redirect it
to 1 which is STDOUT. Now 1＞&2 will redirect the file descriptor 1 to STDERR
and 2＞&3 will redirect file descriptor 2 to 3 which is STDOUT.
So basically you switched STDOUT and STDERR, these are the steps:
 1. Create a new fd 3 and point it to the fd 1
 2. Redirect file descriptor 1 to file descriptor 2. If we wouldn't have
 saved the file descriptor in 3 we would lose the target.
 3. Redirect file descriptor 2 to file descriptor 3. Now file descriptors one
 and two are switched.
Now if the program prints something to the file descriptor 1, it will be
printed to the file descriptor 2 and vice versa.

== This solution helped 21 people ==
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 http://www.freebsd.org/cgi/
man.cgi?query=dialog&apropos=0&sektion=0&manpath=FreeBSD+9.0-
RELEASE&arch=default&format=html, 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 https://unix.stackexchange.com/questions/37660/order-of-redirections/
37662#37662

== This solution helped 1 person ==
The script writer defined fd 3 such as:
exec 3＜＞ File.txt
Open "File.txt" and assign fd 3 to it. Maximum file descriptors: 255
read -n 4 ＜&3
Read only 4 characters.
echo -n . ＞&3
Write a decimal point there.
exec 3＞&-
Close fd 3.
cat File.txt
With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user Ulrich Dangel (https://unix.stackexchange.com/users/8979), user Mikel (https://unix.stackexchange.com/users/3169), user jsterr (https://unix.stackexchange.com/users/20434), user Jonathan Jackson (https://unix.stackexchange.com/users/334746), user Braiam (https://unix.stackexchange.com/users/41104), and the Stack Exchange Network (http://unix.stackexchange.com/questions/42728). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.