Fundamentals 13 min read

Master Bash Bracket Syntax: Parentheses, Brackets, and Braces Explained

This guide explains the different types of brackets used in Bash—parentheses, square brackets, double brackets, and braces—covering their syntax, typical use cases such as command grouping, arithmetic evaluation, array handling, test expressions, parameter expansion, and pattern substitution with clear examples.

ITPUB
ITPUB
ITPUB
Master Bash Bracket Syntax: Parentheses, Brackets, and Braces Explained

Parentheses ()

1. Single parentheses

Command grouping: commands inside () run in a subshell, so variables defined there are not available later. Separate multiple commands with semicolons; the final command may omit the trailing semicolon.

Command substitution: $(cmd) or (cmd) runs cmd and replaces the expression with its standard output. Not all shells support this form (e.g., tcsh).

Array initialization example: array=(a b c d) 2. Double parentheses (( ))

Integer arithmetic expansion: ((exp)) evaluates an arithmetic expression using C‑style syntax. The exit status is 0 (true) for non‑zero results and 1 (false) for zero.

All C‑compatible operators, including ternary ?:, are allowed. Example: echo $((16#5f)) prints 95 (hexadecimal to decimal).

Variable reassignment: a=5; ((a++)) increments a to 6.

Arithmetic comparisons can omit the $ prefix inside (( )). Example: if (( i < 5 )); then …; fi.

Square Brackets []

1. Single brackets

Built‑in [ command (alias of test) evaluates expressions and returns an exit status.

String comparison operators: == and !=. Integer comparison uses -eq, -gt, etc.; relational symbols < and > are not valid for integers.

Logical AND/OR inside [ ] are expressed with -a and -o.

In array contexts, ${array[2]} accesses the element at index 2.

2. Double brackets [[ ]]

Keyword, not a separate command; provides more powerful test syntax.

No pathname expansion or word splitting inside; parameter expansion and command substitution still occur.

Supports pattern matching with =~ and regular expressions.

Logical operators && and || work directly, avoiding the need for -a / -o. Example: if [[ $a != 1 && $a != 2 ]]; then …; fi.

Returns a single exit status for the whole expression.

if ( $i<5 )
if [ $i -lt 5 ]
if [ $a -ne 1 -a $a != 2 ]
if [ $a -ne 1 ] && [ $a != 2 ]
if [[ $a != 1 && $a != 2 ]]
for i in $(seq 0 4); do echo $i; done
for i in `seq 0 4`; do echo $i; done
for ((i=0;i<5;i++)); do echo $i; done
for i in {0..4}; do echo $i; done

Braces { }

1. Brace expansion (globbing)

Comma‑separated list: touch {a,b}.txt creates a.txt and b.txt.

Sequence expansion: touch {a..d}.txt creates a.txt b.txt c.txt d.txt.

# ls {ex1,ex2}.sh
ex1.sh ex2.sh
# ls {ex{1..3},ex4}.sh
ex1.sh ex2.sh ex3.sh ex4.sh
# ls {ex[1-3],ex4}.sh
ex1.sh ex2.sh ex3.sh ex4.sh

2. Command grouping with braces

Creates an anonymous block that runs in the current shell, so variables remain available after the block.

Commands must be separated by semicolons, and the final command requires a trailing semicolon. A space is required after the opening brace.

Parameter Expansion Patterns

Common forms:

${var:-string}   # Use <code>string</code> if <code>var</code> is unset or null
${var:+string}   # Use <code>string</code> only if <code>var</code> is set and non‑null
${var:=string}   # Assign <code>string</code> to <code>var</code> if it is unset, then expand
${var:?string}   # Print <code>string</code> to stderr and abort if <code>var</code> is unset

These can be combined with pattern‑matching operators:

${var%pattern}   # Remove shortest match of <code>pattern</code> from the end
${var%%pattern}  # Remove longest match of <code>pattern</code> from the end
${var#pattern}   # Remove shortest match of <code>pattern</code> from the beginning
${var##pattern}  # Remove longest match of <code>pattern</code> from the beginning

Examples:

# var=testcase
# echo $var
testcase
# echo ${var%s*e}
testca
# echo ${var%%s*e}
te
# echo ${var#?e}
stcase
# echo ${var##?e}
stcase
# echo ${var##*e}
case

Other $‑prefixed Constructs

${a}

expands variable a; braces can be omitted when unambiguous. $(cmd) performs command substitution, equivalent to backticks `cmd`. Some shells (e.g., tcsh) may not support this form. $((expression)) evaluates an arithmetic expression using C syntax, supporting ternary and logical operators.

Practical Usage Tips

Multiple commands can be run in a subshell with (cmd1; cmd2; cmd3) or in the current shell with { cmd1; cmd2; cmd3; }. The latter requires a space after the opening brace and a trailing semicolon.

Redirections placed inside the parentheses affect only the command they follow; redirections placed outside affect all commands inside the group.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

shellscriptingbracketsparameter expansion
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.