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
while IFS= read -r line; do
printf '%s\n' "$line"
if IFS= read -r line; then printf '%s\n' "$line" >&3; fi
done >odd.txt 3>even.txt
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.
{ while … done | odd-filter >filtered-odd.txt; } 3>&1 | even-filter >filtered-even.txt
Another, simpler use case is filtering the error output of a commandfiltering 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 between exec M>&N and somecommand M>&N. The exec form is more powerful in that it does not have to be nested:
exec 8<&0 9>&1
exec >output12
command1
exec <input23
command2
exec >&9
command3
exec <&8
Other examples that may be of interest:
- What does “3>&1 1>&2 2>&3” do in a script?What does “3>&1 1>&2 2>&3” do in a script? (it swaps stdout with stderr)
- File descriptors & shell scriptingFile descriptors & shell scripting
- How big is the pipe buffer?How big is the pipe buffer?
- Bash script testing if a command has run correctlyBash script testing if a command has run correctly
And for even more examples:
- questions tagged
io-redirectionquestions taggedio-redirection - questions tagged
file-descriptorsquestions taggedfile-descriptors - search for examples on this site in the Data Explorer (a public read-only copy of the Stack Exchange database)
P.S. This is a surprising question coming from the author of the most upvoted post on the site that uses redirection through fd 3most upvoted post on the site that uses redirection through fd 3!