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 control operator used to append the output of a command to a file. Unlike the > operator, which truncates the target file before writing, >> instructs the shell to open the target file in append mode (using the O_APPEND system flag). This ensures that existing data is preserved and new data is written strictly to the end of the file. If the specified target file does not exist, the shell creates it, subject to the current umask settings.

Syntax and File Descriptors

In Bash, redirection operators interact directly with File Descriptors (FDs). The general syntax is [n]>>word, where n is an optional file descriptor (which can be multi-digit) and word is the target file path. If n is omitted, Bash defaults to FD 1 (Standard Output, or stdout). Additionally, Bash supports dynamic file descriptor allocation using the {varname}>>word syntax. This instructs the shell to automatically find an available file descriptor (typically 10 or higher) and assign its integer value to the specified variable.

# Default behavior: Appends stdout (FD 1) to the file. stderr (FD 2) is unaffected.
command >> filename


# Explicitly appends stdout. Functionally identical to the above.
command 1>> filename


# Appends stderr (FD 2) to the file. stdout is unaffected.
command 2>> filename


# Appends a multi-digit file descriptor to the file.
command 10>> filename


# Appends both stdout (FD 1) and stderr (FD 2) to the file. (Bash 4.0+)
command &>> filename


# Dynamically allocates an available file descriptor to the variable 'my_fd' and opens it for appending.
exec {my_fd}>> filename

Redirection Order and Chaining

When managing multiple file descriptors, the order of redirection evaluation is strictly left-to-right. To append both stdout and stderr to the same file in older POSIX-compliant shells (prior to the introduction of &>>), file descriptor duplication is required:

# Appends stdout to filename, then redirects stderr to the current location of stdout
command >> filename 2>&1

Technical Characteristics

  • Atomicity: Because >> utilizes the O_APPEND system flag, the operating system guarantees that the file offset is updated atomically to the end of the file before every write operation occurs. This behavior is governed by the OS filesystem implementation and write() system call semantics. This prevents data corruption or interleaved characters when multiple processes append to the same regular file concurrently.
  • Interaction with noclobber: The Bash shell option set -o noclobber (or set -C), which prevents the > operator from overwriting existing files, has no effect on the >> operator. Because >> is inherently non-destructive to existing content, the shell permits its execution regardless of the noclobber state.
  • Expansion: The word (target file) following the >> operator is subject to brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, quote removal, pathname expansion, and word splitting. If the expansion results in more than one word, Bash will throw an ambiguous redirect error.
Master Bash with Deep Grasping Methodology!Learn More