The descriptor table
The kernel keeps a table of open files for each process. An entry in the table is a file description, which records the file, the current offset, and the access mode. A file descriptor is the small integer index that names one entry in this table. Programs use the index in every read and write call, and the kernel does the lookup.
Descriptors are inherited. When a process calls fork, the child gets a copy of the table, and the entries refer to the same open files. That inheritance is how a child can use the standard streams the shell arranged for it, without any further setup by the program.
ls -l /proc/$$/fdEach entry is a symbolic link. Its name is the descriptor number and its target is the file it refers to. This is a direct view of the table the kernel keeps for the shell, and you can use the same view for any process whose number you know.
What the shell does for a redirection
When the shell processes cmd > out.txt, it forks a child. In the child, before calling execve, it opens out.txt for writing with truncation, which returns the lowest free descriptor. It then makes descriptor 1 refer to the same file description. The shell uses dup2 for this, because dup2 copies one descriptor onto a chosen number and closes the old target if necessary.
The sequence is simple, and it explains the ordering rules from the previous lessons. Each redirection is a dup2 that runs in the order written, so a later one copies whatever the earlier one left in place.
exec 3>trace.txt
echo via-fd3 >&3
exec 3>&-
cat trace.txtThe first line opens trace.txt as descriptor 3 in the current shell. The second writes into it through the descriptor. The third closes descriptor 3, and the final cat prints via-fd3. The exec builtin with no command changes the shell own descriptors, which is the same mechanism the child uses.
Duplicating with >&
The syntax >&n asks the shell to call dup2 so that the target descriptor refers to descriptor n. The example below uses that to send a message to standard error, which is descriptor 2, from a shell that otherwise prints to standard output.
echo diagnostic >&2 2> problems.txt
cat problems.txtThe message is written to descriptor 2, which the redirection 2> problems.txt had already pointed at the file. The shell processed the redirections in order, so the echo wrote to the file. Reading the descriptor table after each step is the easiest way to verify your understanding.
Why pipes work the same way
A pipe is created with the pipe system call, which returns two descriptors, one for the read end and one for the write end. The shell forks each command in the pipeline, and in each child it uses dup2 to put the appropriate pipe end on descriptor 0 or 1. The programs then read and write their standard streams exactly as before. They do not know a pipe is involved.
This is the reason the abstraction is powerful. The same read and write calls work for a terminal, a regular file, a socket, or a pipe, because the program only sees a descriptor. Redirection and pipelines are simply different ways of choosing what sits behind slots 0, 1, and 2.
The lab shows descriptor 255 for the shell. Bash moves its own working copy of the terminal to a high number so that it can restore the standard slots for each command. Programs do not normally see that descriptor.
Closing descriptors
A child inherits every open descriptor unless it is closed. Leaking a descriptor into a long-running program can keep a file or a pipe open longer than intended, and a reader may then never see end of file. Bash closes descriptors it opened for a redirection after the command finishes, which is why the cleanup usually happens without thought. The >&- syntax closes a descriptor explicitly.
exec 4>&-
ls -l /proc/$$/fd | grep -c " 4 ->"The count is 0, because descriptor 4 was never opened in this shell. Closing a descriptor that is not open is harmless, which is a useful property when writing cleanup code.
What to take from it
Redirection, pipes, and here-documents all reduce to the same kernel operations: open a file or create a pipe, duplicate a descriptor onto a standard slot, then execute the program. The shell syntax is a readable front end. When something unexpected happens in a pipeline, asking which descriptor points where, and in what order the copies happened, will almost always give you the answer.