I know you can create a file descriptor and redirect output to it. e.g.
exec 3<> /tmp/foo # open fd 3.
echo a >&3 # write to it
exec 3>&- # close fd 3.
But you can do the same thing without the file descriptor:
FILE=/tmp/foo
echo a > "$FILE"
I'm looking for a good example of when you would have to use an additional file descriptor.
Best Answer
Most commands have a single input channel (standard input, file descriptor 0) and a single output channel (standard output, file descriptor 1) or else operate on several files which they open by themselves (so you pass them a file name). (That's in addition from standard error (fd 2), which usually filters up all the way to the user.) It is however sometimes convenient to have a command that acts as a filter from several sources or to several targets. For example, here's a simple script that separates the odd-numbered lines in a file from the even-numbered ones
Now suppose you want to apply a different filter to the odd-number lines and to the even-numbered lines (but not put them back together, that would be a different problem, not feasible from the shell in general). In the shell, you can only pipe a command's standard output to another command; to pipe another file descriptor, you need to redirect it to fd 1 first.
Another, simpler use case is filtering the error output of a command.
exec M>&N
redirects a file descriptor to another one for the remainder of the script (or until another such command changes the file descriptors again). There is some overlap in functionality betweenexec M>&N
andsomecommand M>&N
. Theexec
form is more powerful in that it does not have to be nested:Other examples that may be of interest:
And for even more examples:
io-redirection
file-descriptors
P.S. This is a surprising question coming from the author of the most upvoted post on the site that uses redirection through fd 3!