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.

A brace group is a command grouping construct in Bash that executes a sequence of commands within the current shell execution environment. Unlike subshells, which spawn a child process via a fork() system call, commands executed within a brace group share the exact same state as the parent shell. Consequently, variable assignments, environment modifications, and working directory changes persist after the group terminates.

Syntax

{ command1; command2; command3; }
Alternatively, using newlines for command separation and termination:
{
    command1
    command2
    command3
}

Lexical and Syntactic Rules

  • Reserved Word Parsing: In Bash, { and } are evaluated as reserved words, not shell operators (like ( or )). Because of this lexical distinction, two strict parsing rules apply:
    1. The opening brace { must be separated from adjacent tokens by whitespace or a shell metacharacter.
    2. The { token is only recognized as a reserved word if it appears as the first word of a simple command, or immediately follows a control structure or metacharacter (such as ;, |, or &). If it appears elsewhere, it is treated as a literal string. For example, echo { ls; } will not execute ls in a brace group; it will simply pass the literal strings {, ls;, and } as arguments to echo.
  • List Termination: The internal command list must be explicitly terminated before the group is closed. A list terminator—such as a semicolon (;), an ampersand (&), or a newline character—must appear after the final command. Whitespace is perfectly valid (and standard) between this terminator and the closing brace }.
  • Command Requirement: A brace group must contain a valid command list. An empty command list inside a brace group (e.g., { } or { ; }) is syntactically invalid and will produce a syntax error.

I/O Redirection Mechanics

When I/O redirection is appended to a brace group, the shell evaluates the redirection once for the entire block rather than per command. The shell opens the specified file descriptor(s) and all commands within the braces inherit that shared file descriptor.

# The file descriptor for output.txt is opened once and shared by both commands
{ command1; command2; } > output.txt

Exit Status

The exit status ($?) of a brace group is strictly determined by the exit status of the last command executed within the enclosed list.
Master Bash with Deep Grasping Methodology!Learn More