Bash File Descriptors: Why FD 10+ for Redirection?
Bash shells often employ file descriptors numbered 10 or higher when handling input/output redirection for standard streams (stdin, stdout, stderr). This behavior might seem arbitrary, especially when compared to how other file descriptors are managed. This article delves into the reasons behind this specific implementation detail in Bash's io-redirection mechanism.

The Curious Case of File Descriptor 10+
When you execute commands in a Bash shell, input/output redirection is a fundamental feature. You might redirect standard output (file descriptor 1) to a file, standard error (file descriptor 2) to another, or standard input (file descriptor 0) from a file. Interestingly, when Bash performs these redirections for built-in commands, it often utilizes file descriptors numbered 10 or greater, specifically the first available descriptor at or above 10. For instance, attempts to check if file descriptor 10 is open before such a redirection will typically show it as closed, indicated by an exit code of 1 from the test -e /proc/$$/fd/10 command. However, after the redirection is in place, this descriptor becomes active.
This behavior is particularly noticeable with built-in commands like test. Unlike external executables, which are forked into separate processes where redirections don't affect the parent shell, Bash built-ins execute within the same process. This means any modification to standard file descriptors (0, 1, 2) needs to be reversible to ensure the shell's state remains consistent after the command completes. The use of a higher-numbered, typically unused, file descriptor to temporarily hold the original standard streams is a key part of this reversibility mechanism.
The question arises: why file descriptor 10 specifically, and not just any unused descriptor? The observed pattern is that Bash seems to reserve a range of higher-numbered file descriptors, starting from 10, for this specific purpose of saving and restoring standard I/O streams during the execution of built-in commands. This article explores the underlying reasons for this design choice in Bash's io-redirection handling.
Bash Built-ins and Process Isolation
Bash, like many shells, has a set of commands that are built directly into the shell executable itself. Examples include echo, cd, pwd, and test. When you run an external command, such as /bin/ls, the shell performs a fork() operation to create a child process. The child process then executes /bin/ls. Any I/O redirection set up for /bin/ls is applied within this child process and does not inherently affect the parent shell's file descriptors.
However, when a Bash built-in command is executed, it runs directly within the parent shell process. If a built-in command were to directly modify standard file descriptors 0, 1, or 2 without a mechanism to restore them, it could lead to unexpected and undesirable side effects. For example, if echo (a built-in) redirected stdout to a file, and that redirection wasn't cleaned up, all subsequent output from the shell itself might go to that file, breaking normal interactive use.
The Save-and-Restore Mechanism
To address the challenge of maintaining shell state when built-ins modify standard I/O, Bash employs a save-and-restore technique. Before executing a built-in command that requires redirection of stdin, stdout, or stderr, Bash duplicates the original file descriptors (0, 1, or 2) into a higher-numbered, usually free, file descriptor. The common starting point for this is file descriptor 10. This process, often implemented using dup2(), effectively creates a backup copy of the original stream.
Once the built-in command has finished executing, Bash uses these saved file descriptors to restore the original stdin, stdout, and stderr. For instance, if stdout (fd 1) was redirected, Bash would use dup2(10, 1) to put stdout back to where it was. This ensures that the shell's I/O state is preserved across the execution of built-in commands, preventing unintended consequences for subsequent shell operations.
Why File Descriptor 10? SHELL_FD_BASE
The specific choice of file descriptor 10 as the starting point for saving standard streams is an implementation detail within Bash. In the Bash source code, this is often referred to by a variable like SHELL_FD_BASE. This value is typically set to 10, indicating the base for temporary file descriptors used by the shell itself for internal operations, such as saving original file descriptors during redirections of built-in commands. The logic is to pick a file descriptor number that is unlikely to be already in use by the program or the environment.
The redir.c file within the Bash source code is where much of this file descriptor manipulation logic resides. The system ensures that file descriptors 0, 1, and 2 are consistently managed. When redirection occurs for a built-in, Bash first checks if fd 10 (or the next available SHELL_FD_BASE + offset) is free. If it is, it uses dup2() to copy the original stdio FD (0, 1, or 2) to this higher FD. This practice avoids conflicts with file descriptors that might legitimately be opened by the command being executed, which could be anything from 3 up to the system's limit.
Handling of Other File Descriptors
The behavior observed with file descriptor 10+ is specific to the redirection of standard input, output, and error (0, 1, and 2). When you redirect other file descriptors, such as 3, 4, or higher, Bash does not necessarily employ this save-and-restore mechanism using a descriptor >= 10. For example, redirecting file descriptor 3 (e.g., 3>&1) does not automatically cause file descriptor 10 to become active in the way that redirecting stdout does.
This distinction highlights that the use of fd 10+ is a specialized technique for managing the shell's own critical standard streams during the execution of its built-in commands. It’s a kludge designed to make built-ins behave more predictably and consistently with external commands regarding I/O redirection, without corrupting the shell’s interactive environment. File descriptors 3 and above are typically treated as user-definable streams for specific command purposes, and Bash doesn't interfere with them unless explicitly instructed to do so, nor does it need to preserve their state for the shell's own integrity.
Trade-offs and Considerations
The approach Bash uses for I/O redirection of built-ins, by employing higher-numbered file descriptors like 10 for saving and restoring, is a clever optimization. It allows built-in commands to perform redirections within the same process without permanently altering the shell's standard streams. This maintains consistency and prevents unexpected behavior in interactive sessions or scripts.
However, this mechanism is a specific implementation detail of Bash. Other shells might use different strategies. Furthermore, it's crucial to understand that this behavior is primarily for built-ins. When dealing with external commands, standard file descriptor redirections (0, 1, 2) are handled by the fork()ed child process, and Bash's internal fd 10 mechanism is not involved. Developers should be aware that attempting to predict or rely on the exact file descriptor number used by Bash for its internal saving could be fragile, as it's an implementation choice that could theoretically change, though SHELL_FD_BASE has been a stable convention.
When This Approach Does Not Fit
The save-and-restore mechanism using file descriptors >= 10 is specifically tied to Bash's internal handling of its own built-in commands and their standard I/O streams (0, 1, 2). This approach does not apply when:
- Executing external commands: Standard I/O redirection is handled in the forked child process, independent of the parent shell's internal temporary file descriptors.
- Redirecting file descriptors other than 0, 1, or 2: If you explicitly open and redirect descriptor 3, 4, or higher using syntax like
3>file, Bash does not automatically reserve fd 10+ to save the original state of descriptor 3. These are treated as user-defined streams. - Complex pipeline scenarios involving multiple shell features: While robust, intricate combinations of process substitution, here-documents, and redirections across multiple commands might have subtle interactions that require careful testing.
Understanding this distinction is key to correctly diagnosing I/O redirection issues in Linux and Bash scripting. The phenomenon of file descriptor 10+ appearing during redirection is a signal that Bash is performing its internal state management for a built-in command, rather than a general rule for all types of I/O redirection.
This article draws on Why does Bash use a file descriptor >=10 on redirection of stdin/stdout/stderr only? and answers to it (1, 2), on the Stack Exchange network, licensed under CC BY-SA 4.0.
- #Programming
- #bash
- #file-descriptors
- #io-redirection
- #linux
- #shell scripting



Comments