Wednesday, 4 March 2026

Understanding Brace Expansion in Bash

 

Understanding Brace Expansion in Bash

Brace expansion is a powerful feature in Bash that allows you to generate multiple strings from a single expression. It is purely string manipulation performed by the shell before the command runs.

It is important to understand that brace expansion does not check if files exist. It only expands the text pattern.


Basic Brace Expansion

Brace expansion works when comma-separated values are placed inside curly braces {}.

Example:

[root@oel01db ~]# echo {hello,world}
hello world
[root@oel01db ~]#

Here the shell expands {hello,world} into:

hello world

before the echo command executes.


When Brace Expansion Does NOT Work

If you replace the comma with a space, brace expansion does not occur.

[root@oel01db ~]# echo {hello world}
{hello world}
[root@oel01db ~]#

Explanation:

  • Brace expansion requires comma-separated values

  • Without a comma, the shell treats it as plain text


Combining Variables with Brace Expansion

Brace expansion can be combined with variables.

[root@oel01db ~]# log_file=mylog
[root@oel01db ~]#
[root@oel01db ~]# echo "$log_file."{log,txt,log}
mylog.log mylog.txt mylog.log
[root@oel01db ~]#

Explanation:

  1. The shell expands {log,txt,log}

  2. Then the variable $log_file is substituted

  3. The final result is:

mylog.log mylog.txt mylog.log

Using Brace Expansion in Scripts

Brace expansion is commonly used inside scripts to generate multiple filenames.

Example script:

[root@oel01db ~]# cat create_files.sh
file_name='my_log'

arr=( "$file_name."{log,txt,xml} )

for item in "${arr[@]}"; do
touch "$item"
ls -1 "$item"
done
[root@oel01db ~]#

Running the script:

[root@oel01db ~]# bash create_files.sh
my_log.log
my_log.txt
my_log.xml
[root@oel01db ~]#

This script:

  1. Uses brace expansion to generate filenames

  2. Stores them in an array

  3. Creates the files using touch


Important Note: Brace Expansion Does NOT Check Files

Brace expansion only performs string manipulation.

It does not verify whether files or directories exist.

Example:

[root@oel01db ~]# cat create_files.sh
file_name='my_log'

arr=( /etc/{foo,bar}/{prod,dev,tst}.log)

for item in "${arr[@]}"; do
echo $item
done
[root@oel01db ~]#

Running the script:

[root@oel01db ~]# bash create_files.sh
/etc/foo/prod.log
/etc/foo/dev.log
/etc/foo/tst.log
/etc/bar/prod.log
/etc/bar/dev.log
/etc/bar/tst.log
[root@oel01db ~]#

Even if /etc/foo or /etc/bar do not exist, the shell still expands the strings.


Nested Brace Expansion

Brace expansion can also be nested.

Example:

echo {A,B}{1,2}

Output:

A1 A2 B1 B2

The shell generates every combination.


Brace Expansion with Number Sequences

Brace expansion also supports numeric sequences.

Example:

echo file{1..5}.txt

Output:

file1.txt file2.txt file3.txt file4.txt file5.txt

This is extremely useful when generating multiple files.

Example:

touch file{1..5}.txt

Brace Expansion with Step Values

You can also define a step value.

Example:

echo {1..10..2}

Output:

1 3 5 7 9

Brace Expansion and Globbing

Brace expansion often works together with globbing (wildcards).

Example:

[root@oel01db ~]# ls *.{txt,xml}
my_log.txt my_log.xml
[root@oel01db ~]#

What actually happens:

  1. Brace expansion runs first

ls *.txt *.xml
  1. Then the wildcard * is processed

  2. The shell searches the directory for matching files


Order of Shell Expansion

Understanding the order is helpful:

  1. Brace Expansion

  2. Variable Expansion

  3. Command Substitution

  4. Globbing (wildcards)

Because brace expansion happens very early, it simply creates strings without checking the filesystem.


Practical Use Cases

Brace expansion is commonly used for:

Creating multiple files

touch log_{dev,prod,test}.txt

Creates:

log_dev.txt
log_prod.txt
log_test.txt

Creating multiple directories

mkdir -p project/{src,bin,docs}

Creates:

project/src
project/bin
project/docs

Bulk file operations

rm *. {log,tmp}

Expands to:

rm *.log *.tmp

Key Takeaways

  • Brace expansion is string generation performed by the shell

  • It requires comma-separated values

  • It does not check if files exist

  • It executes before most other shell expansions

  • It is extremely useful for automation and scripting

No comments:

Post a Comment

JFrog Artifactory - How to install

JFrog Artifactory OSS Installation Guide CentOS 9 + PostgreSQL 17 This guide provides a structured workflow to install JFrog Artifactory OSS...