1

recently came across several chained redirections to file descriptors as shown is the snippet below:

name=$(dialog --inputbox "Please enter your phone number." 10 30 3>&1 1>&2 2>&3 3>&1) || exit

... while I understand this script is

  1. Creating a new file-descriptor 3 which is is then redirecting to STDOUT,
  2. Redirecting STDOUT to STDERR
  3. STDERR to the file-descriptor 3
  4. Finally, redirecting file-descriptor 3 again to STDOUT

My question is why is this being done? It looks kind of circular to me, so it would be great if someone could explain the logic or possible purpose/use-case for wanting to chain the redirections this way.

TIA!

computronium
  • 778
  • 6
  • 15
  • 3
    Does this answer your question? [What does "3>&1 1>&2 2>&3" do in a script?](https://unix.stackexchange.com/questions/42728/what-does-31-12-23-do-in-a-script) Regarding "why", I think you will need to ask who wrote the script. – Quasímodo Sep 08 '20 at 14:20
  • @Quasímodo: I read that before posting. but thanks anyway. – computronium Sep 08 '20 at 14:22
  • 2
    Perhaps if you put the word "current" before each STDOUT and STDERR in your steps 1 to 4 it would make more sense to you? – icarus Sep 08 '20 at 14:53
  • 1
    The answer is the same as in Quasimodo’s suggestion above: it swaps stdout and stderr. The final `3>&1` doesn’t serve any purpose. – Stephen Kitt Sep 08 '20 at 15:18
  • that's precisely what i was wondering. thanks @StephenKitt – computronium Sep 08 '20 at 15:28

0 Answers0