Bash: file descriptors Bash: file descriptors bash bash

Bash: file descriptors


fd3 seems to get "depleted" after "consumption", is it also automatically closed after first use?

No, it is not closed. This is due to the way exec works. In the mode in which you have used exec (without arguments), its function is to arrange the shell's own file descriptors as requested by the I/O redirections specified to itself, and then leave them that way until the script terminated or they are changed again later.

Later, cat receives a copy of this file descriptor 3 on its standard input (file descriptor 0). cat's standard input is implicitly closed when cat exits (or perhaps, though unlikely, cat closes it before it exists, but that doesn't matter). The original copy of this file, which is the shell's file descriptor 3, remains. Although the actual file has reached EOF and nothing further will be read from it.

how is fd3 different from a named pipe? (something I have looked at already)

The shell's <(some command) syntax (which is not standard bourne shell syntax and I believe is only available in zsh and bash, by the way) might actually be implemented using named pipes. It probably isn't under Linux because there's a better way (using /dev/fd), but it probably is on other operating systems.

So in that sense, this syntax may or may not be a helper for setting up named pipes.

when exactly does the command yes start executing? upon fd declaration? later?

As soon as the <(yes violet) construct is evaluated (which happens when the exec 5< <(yes violet) is evaluated).

does yes stop (CTRL-Z or other) and restart when more violet is needed?

No, it does not stop. However, it will block soon enough when it starts producing more output than anything reading the other end of the pipe is consuming. In other words, the pipe buffer will become full.

how can I get the PID of yes?

Good question! $! appears to contain it immediately after yes is executed. However there seems to be an intermediate subshell and you actually get the pid of that subshell. Try <(exec yes violet) to avoid the intermediate process.

can I get a list of "active" fds?

Not from the shell. But if you're using an operating system like Linux that has /proc, you can just consult /proc/self/fd.

very interesting race condition on filtering through fd4, can it be avoided?

To avoid it, you presumably want to wait for the grep process to complete before proceeding through the script. If you obtain the process ID of that process (as above), I think you should be able to wait for it.

will yes only stop when I exec 5>&-?

Yes. What will happen then is that yes will continue to try to produce output forever but when the other end of the file descriptor is closed it will either get a write error (EPIPE), or a signal (SIGPIPE) which is fatal by default.

does it matter whether I close with >&- or <&-?

No. Both syntaxes are available for consistency's sake.