0

I've read (e.g. here: How do file descriptors work?) that to use file descriptors 3 - 9 I need to open them first. Trying to use them without it produces an error:

function f()
{
    echo hi 1>&3
}
f

Output:

bash: 3: Bad file descriptor

However, if I redirect it when calling the function everything seems to work fine:

f 3>&1

Output:

hi

So do I need to open file descriptors or not?

NPS
  • 6,003
  • 11
  • 53
  • 90

1 Answers1

1

Yes you do need to open a new file descriptor and as a good practice you should close it in the end like this:

f() {
    # open fd=3 redirecting to 1 (stdout)
    exec 3>&1

    # redirect stdout to fd=3
    echo hi >&3

    # close fd=3
    exec 3>&-
}

Note that when you run f 3>&1 while invoking function you are actually opening file descriptor 3 by redirecting it to &1 (stdout).

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • If I do `f 3>&1 ; f` then the 2nd call to `f` still returns error - file descriptor 3 isn't open at that point. So I still don't understand how this works. – NPS Aug 27 '20 at 11:29
  • Also shouldn't you open and close `3` outside of `f`? I know it's just an example but as it is your definition of `f` doesn't seem to make much sense (it effectively prints to stdout anyway). – NPS Aug 27 '20 at 11:34
  • Well we require an extra fd when we want to treat it differently. e.g. some folks want to redirect all stdout/stderr to a log file while still want to keep freedom of writing some progress etc to terminal. Since we didn't redirect stdout/stderr to any log file so here `$3` is just a copy of `&1`. – anubhava Aug 27 '20 at 13:33