Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does `exec {logOutFd}>&1 {logErrFd}>&2` do?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading