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 a Bash redirection operator that appends the output of a command to a file. When invoked, the shell opens the target file in append mode (utilizing the O_APPEND flag at the system call level). If the target file exists, the file offset is positioned at the end of the file prior to writing, preserving all existing data. If the file does not exist, the shell creates it.

Syntax

[n]>> word
  • n: An optional integer representing the file descriptor (FD) to redirect. If omitted, Bash defaults to 1 (standard output / stdout).
  • word: The target destination. The shell subjects this to brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, word splitting, pathname expansion (globbing), and quote removal to determine the final filename.
Ambiguous Redirects: If the expansions applied to word result in more than one word (for example, redirecting to an unquoted variable containing spaces, or a glob pattern that matches multiple files), Bash will fail the command and throw an “ambiguous redirect” error.

File Descriptor Targeting

Because >> operates on file descriptors, it can be prefixed with specific FD integers to control exactly which output stream is appended to the target file.

# Appends standard output (FD 1) implicitly
command >> filename


# Appends standard output (FD 1) explicitly
command 1>> filename


# Appends standard error (FD 2) explicitly
command 2>> filename

Multi-Stream Appending

To append multiple file descriptors to the same file, Bash provides specific syntactic constructs. Bash 4.0+ Syntax: The &>> operator is a dedicated redirection operator that appends both standard output (FD 1) and standard error (FD 2) simultaneously.
command &>> filename
POSIX-Compliant Syntax: To achieve the same result in older Bash versions or strictly POSIX-compliant scripts, standard error must be redirected to standard output, and standard output must be appended to the file. The order of evaluation is strictly left-to-right.
command >> filename 2>&1

Execution Mechanics

  1. Parsing: The shell parses the command line and identifies the >> redirection operator.
  2. File Operations: Before the command is executed, the shell evaluates word to resolve the filename. It then issues an open() system call on that file with the O_WRONLY | O_CREAT | O_APPEND flags.
  3. Duplication: The shell uses dup2() to duplicate the opened file descriptor onto the targeted file descriptor (e.g., FD 1).
  4. Execution: The command executes, completely unaware of the redirection, writing its output to the file descriptor which now points to the end of the target file.
Master Bash with Deep Grasping Methodology!Learn More