Bash Brackets Explained in Simple Words (With 8 Examples)

When learning Bash scripting, one thing that often confuses beginners is the use of different brackets and symbols. You may see (), {}, [], [[]], $(), ${}, or (()) and wonder why Bash needs so many of them.

We’ll break down these symbols with clear explanations and simple examples so you can confidently read and write Bash scripts.

ALSO READ:

Click here to go to the GitHub repos link

$() – Run a Command and Use Its Output

The $() syntax runs a command and stores its output.

today=$(date)
echo "Today is $today"

Here, the date command runs first, and its output is saved in the variable today.

Use $() when you need the result of a command inside your script.

() – Subshell and Arrays

Subshell

Commands inside () run in a separate shell.

(cd /var/log && ls)

Any directory change inside the parentheses does not affect your current shell.

Useful when you want to isolate commands.

Arrays

Parentheses are also used to create arrays.

files=(app.log error.log access.log)

for file in "${files[@]}"; do
  echo "Processing $file"
done

Arrays let you store multiple values in one variable.

{} – Group Commands and Create Sequences

Grouping Commands

Curly braces group commands that run in the same shell.

{
  echo "Starting task"
  echo "Task running"
  echo "Task completed"
}

Variables created inside remain available afterward.

Brace Expansion

Brace expansion quickly creates multiple files or strings.

touch file{1..3}.txt

This creates:

file1.txt file2.txt file3.txt

Very useful for bulk operations.

$var and ${var}—Using Variables

name="John"
echo "$name"
echo "${name}_admin"

${} is safer when you attach extra text to a variable.

${} – Modify Variable Values

Bash allows you to modify variables without using external commands.

file="report.txt"
backup="${file%.txt}.bak"
echo "$backup"

Output:

report.bak

Commonly used for renaming files and string manipulation.

[] – Basic Condition Check

Single brackets are used to test conditions.

file="/etc/passwd"

if [ -f "$file" ]; then
  echo "File exists"
fi

This is the traditional way of writing conditions in shell scripts.

[[]] – Advanced Condition Check (Recommended)

Double brackets are more powerful and safer in Bash.

if [[ $USER == "root" ]]; then
  echo "You are root"
fi

Benefits:

  • Supports pattern matching
  • Handles logical operators better
  • Fewer quoting issues

(()) – Math Operations

Double parentheses are used for arithmetic.

a=5
b=3
result=$((a * b + 1))
echo "$result"

Only works with numbers, not strings.

SymbolWhat it does
$()Command output
()Subshell / Array
{}Group commands / Expansion
$varRead variable
${}Modify variable
[]Basic condition
[[]]Advanced condition
(())Arithmetic
Tech Base Hub
Tech Base Hub

Author:
Krishna T
Linux | DevOps | Cloud Engineer

Leave a Comment