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 shell redirection control operator that instructs the Bash shell to redirect the standard output (stdout, file descriptor 1) of a command into a specified file. When evaluated, the shell opens the target file for writing, creating the file if it does not exist, or truncating it to zero length if it already exists.

Syntax

command > filename
Because stdout is the default file descriptor for this operator, the above is syntactically identical to explicitly declaring file descriptor 1:
command 1> filename

Technical Mechanics

  • Execution Order: Redirection is handled entirely by the shell before the command is executed. The shell performs an open() system call on the target file with the flags O_WRONLY | O_CREAT | O_TRUNC. Consequently, if you redirect to an existing file, its contents are destroyed before the command even begins processing.
  • Empty Commands: Because the shell processes the redirection first, executing > filename with no preceding command is a valid, highly efficient way to truncate an existing file to zero bytes or create an empty file.
  • File Descriptor Isolation: The > operator strictly redirects stdout. Standard error (stderr, file descriptor 2) remains attached to the controlling terminal unless explicitly redirected.

Variations and Modifiers

The > operator serves as the foundation for several other redirection constructs in Bash:
  • Specific File Descriptors: Prepending a number redirects that specific file descriptor.
command 2> filename  # Redirects stderr only
  • Combined Output: Bash provides syntax to redirect both stdout and stderr to the same file simultaneously.
command &> filename
# Equivalent POSIX-compliant syntax:
command > filename 2>&1
  • Clobber Override (>|): Bash includes a shell option called noclobber (set -o noclobber or set -C). When enabled, the shell will refuse to truncate an existing file using the standard > operator, returning an error instead. The >| operator forces the redirection, bypassing the noclobber restriction and truncating the file anyway.
command >| filename
Master Bash with Deep Grasping Methodology!Learn More