1

I don't understand this line:

exec {logOutFd}>&1 {logErrFd}>&2

There are no variable dereferences, and it seems to be just a no-op, like running exec with no parameters. What's going on?

l0b0
  • 55,365
  • 30
  • 138
  • 223

2 Answers2

4

exec with no parameters is used to start redirections that last for the rest of the script.

From the Bash manual section on redirection:

Each redirection that may be preceded by a file descriptor number may instead be preceded by a word of the form {varname}. In this case, for each redirection operator except >&- and <&-, the shell will allocate a file descriptor greater than 10 and assign it to {varname}.

So this is duplicating stdout and stderr to new file descriptors, and setting the variables $logOutFd and $logErrFd to these descriptors, respectively.

This allows later code that executes while stdout or stderr are redirected to write to the original stdout or stderr, with

echo stdout message >&$logOutFd
Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

Reviewing other preceding script: https://github.com/search?q=repo%3ANixOS%2Fnixpkgs%20%20logOutFd&type=code

enter image description here

And reading:

They are using complex i/o redirections to return the stream pointer from some files previously open (8 and 9 position) to the classic stdout and stderr

exec 8>&1 9>&2
JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • Neither https://stackoverflow.com/a/7082184/3957754 nor https://brendanzagaeski.appspot.com/0009.html explain the syntax in the question though. At least, I could find no mention of the same syntax or the word "curly". – l0b0 Jun 09 '23 at 01:43