Skip to main content

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.

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

[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.

# 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
Master Bash with Deep Grasping Methodology!Learn More