> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Bash Redirect Input

The `<` operator is the standard input redirection operator in Bash. It instructs the shell to open a specified file for reading and map its contents to the standard input (stdin, file descriptor `0`) of a command, replacing the default input stream (typically the terminal keyboard).

## Syntax

```bash theme={"dark"}
[n]<word
```

* **`n`**: An optional integer representing the file descriptor to be modified. If omitted, Bash defaults to `0` (standard input).
* **`word`**: The target file to be opened. Before redirection occurs, `word` is subject to brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, word splitting, pathname expansion (globbing), and quote removal.

## Underlying Mechanics

1. **Parsing and Evaluation**: Redirection is processed by the shell *before* the associated command is executed. The shell parses the command line, identifies the `<` operator, and evaluates `word` to resolve the target filepath.
2. **System Calls**: The shell invokes the `open()` system call on the resolved `word` with the `O_RDONLY` (read-only) flag.
3. **File Descriptor Duplication**: If the file opens successfully, the shell uses the `dup2()` system call to duplicate the newly obtained file descriptor onto file descriptor `0` (or the explicitly provided `n`).
4. **Command Execution**: The method of execution depends on the command type:

   * **External Commands**: The shell forks a child process, applies the file descriptor modifications, and invokes an `exec` system call to run the command.
   * **Shell Builtins and Functions**: The shell temporarily duplicates and saves the original file descriptors, applies the redirection within the current process, executes the builtin or function, and then restores the original file descriptors without invoking `exec`.

   In all cases, the executing command remains entirely unaware of the redirection; it simply reads from its standard input, which the shell has wired to the file.

## Failure States

If the redirection fails prior to command execution, the shell aborts the command and returns a non-zero exit status. Common failure conditions include:

* **Ambiguous redirect**: The expansions applied to `word` result in more than one word (e.g., `command < *.txt` when multiple matching files exist).
* **File not found**: The resolved `word` does not exist (`ENOENT`).
* **Permission denied**: The executing user lacks read permissions for the file (`EACCES`).
* **Is a directory**: The resolved `word` points to a directory rather than a file (`EISDIR`).

## Explicit File Descriptor Mapping

While typically used without the optional `n` prefix, the operator can be bound to custom file descriptors for advanced stream manipulation.

```bash theme={"dark"}

# Implicitly maps to fd 0 (stdin)
command < file.txt


# Explicitly maps to fd 0 (functionally identical to the above)
command 0< file.txt


# Opens file.txt for reading and assigns it to custom file descriptor 3
command 3< file.txt
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Bash Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
