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 an output redirection operator in Bash that instructs the shell to route a stream from a specific file descriptor to a target file or device. By default, it redirects standard output (stdout, file descriptor 1). When invoked, the shell performs an open() system call on the target file with the O_WRONLY | O_CREAT | O_TRUNC flags. This means the file is created if it does not exist, and its size is truncated to zero bytes before any new data is written.

Syntax

[n]> word
  • [n]: An optional integer representing the file descriptor to redirect. If omitted, Bash defaults to 1 (stdout).
  • >: The redirection operator.
  • word: The target destination. The shell subjects this string to brace expansion, tilde expansion, parameter expansion, command substitution, arithmetic expansion, word splitting, pathname expansion (globbing), and quote removal to determine the final file path before the redirection is executed. If the result of these expansions yields more than one word, Bash will fail the redirection with an “ambiguous redirect” error.

File Descriptor Variations

The operator can be modified by prefixing it with specific file descriptors or by using Bash-specific combined redirection syntax:
1> word    # Explicitly redirects stdout (identical to > word)
2> word    # Redirects standard error (stderr, file descriptor 2)
&> word    # Bash extension: Redirects both stdout and stderr to the file 'word'
>& word    # Redirects both stdout and stderr, with conditional file descriptor duplication
While &> and >& often appear similar, their parsing rules differ significantly. The &> operator always treats word as a filename (e.g., &>2 creates a file literally named 2). In contrast, if the word following >& evaluates to a number or a hyphen (-), >& acts as a file descriptor duplication or closing operator (e.g., >&2 duplicates stdout to stderr). It only falls back to redirecting both streams to a file if word does not evaluate to a number or hyphen.

The noclobber Option and >|

Because the default behavior of > truncates existing files, Bash provides the noclobber shell option (set -C or set -o noclobber) to prevent accidental data loss. When noclobber is enabled, Bash will refuse to redirect output to an existing regular file and will return an error. To forcefully override the noclobber restriction and truncate the file regardless of the shell option, the >| operator is used:
>| word    # Bypasses noclobber and forces truncation of the file

Execution Order and Evaluation

Redirections are processed by the shell from left to right before the command itself is executed. If the shell fails to open the target file specified by the > operator (e.g., due to insufficient permissions, a read-only filesystem, or an invalid path), the redirection fails, an error is printed to stderr, and the associated command is aborted before execution.
Master Bash with Deep Grasping Methodology!Learn More