How to use Brace Expansion in Linux Bash Shell

Contents

A terminal window on a Linux system.

Brace expansion is a useful technique for generating string lists that can be used in scripts and aliases and on the Linux command line.. Save time and avoid mistakes by typing less.

Clamp expansion

Before Bash's shell run a command in a terminal window or a line in a script, check if you need to do any substitution in the command. Variable names are replaced by their values, aliases are replaced by the commands for which they are abbreviated and any expansion is performed. Key expansion is a form of expansion supported by Bash.

Clamp expansion is enabled on modern housings, but it is possible that it is missing in some old cases. If you are going to use brace expansion in your scripts, make sure to invoke a shell that supports key expansion, like bash:

We will use Bash for our examples.

String list generation may seem more like a novelty than a benefit, but it offers some features that can save time and keystrokes. Often, can provide a simple and elegant solution to an obstacle or requirement.

Simple expansions

A key expansion is contained between a pair of keys “{}”. Can be a comma separated list of items or a range specifier. No spaces inside the keys are allowed unless you have wrapped the chain in quotation marks “". “

For a comma separated list, the expansion procedure takes each element in turn and passes it to the call command. In this example, it is echo which just prints them to the terminal window. Note that commas are ignored.

echo {one,two,three,four}

A list can be made up of words or digits.

echo {1,2,3,4}

The order of the items in the list is absolutely arbitrary.

echo (4,2,3,1)

an expansion range has a start and end character connected with colons ” .. ”Without any blank space. Expansion automatically provides all missing list items, so the entire range is created from the start character to the end.

This will print the digits of the 1 al 10.

echo {1..10}

The numbering is arbitrary. You don't have to start at one.

echo {3..12}

Ranges can be specified to run backward. This will generate a five-to-one list.

echo {5..1}

Ranges can include negative numbers.

echo {4..-4}

As we previously pointed out, a range has a beginning and an end character. It doesn't have to be a number. It can be a letter.

echo {q..v}

Letters can also go backwards.

echo {f.. to}

Using looped corset expansion

You can use key expansion with looped ranges in scripts.

for i in {3..7}
do
   echo $i
done

key expansion ranges allow you to use characters as a loop variable.

for i in {m..q}
do
   echo $i
done

Loops are generally used in scripts, but there is nothing to stop you from typing them on the command line to see what happens.

for i in {3..7}; do echo $i; done

for i in {m..q}; do echo $i; done

Concatenation and nesting

Two adjacent expansions do not act independently one after the other. Interoperan. Each element in the first expansion is acted upon by each element in the second expansion..

echo {q..v}{1..3}

Expansions can also be nested. A nested expansion will act on the immediately preceding element.

echo {part-1,part-2{a,b,c,d},part-3}

Additionally you can nest expansions by creating a comma delimited list of range expansions.

echo {{5..0},{1..5}}

Preamble and postscript

You can place text before and after a brace expansion so that that text is included in the expansion results. Text placed in front of an expansion is called a preamble, while the text placed behind a brace expansion is called a postscript.

This command uses a preamble.

echo chapter{1..3}

This example uses a postscript:

echo {contents,paper,bilbiography}.md

And this command uses both.

echo chapter-{1..4}.md

Directory extension and file names

As you probably already guessed, one of the main uses of brace expansions is to create file and directory names that can be passed to other commands. We have been using echo as a convenient way to see exactly what happens when an expansion is triggered. You can substitute any command that takes file or directory names as input and use brace expansion with it.

To quickly create some files, use touch:

touch file-{1..4}.txt
ls *.txt

If you have many files with the same base name but different file extensions and you want to perform an operation on a subset of them, key expansions can contribute. Here, we are compressing a subset of files that have “Program” as a base name in a ZIP file named “source-code.zip”.

Development directories contain many files that will have the same base name as your main program. As usual, you do not want to backup or distribute files as object files “.O”. This is a simple way to include only the file types of interest.

zip source-code program{.c,.h,.css}

This command will make a copy of a file and add to it “.behind”, backing up the original file. An interesting point to pay attention is that the brace expansion contains a comma separated list, but the first element is empty. If we hadn't included the comma, expansion would not have taken place.

cp brace/new/prog-1.c{,.behind}
ls brace/new/prog-1.c.bak

To perform any action on two files in different directories, we can use a key expansion in the path to the files.

In this example, the directory “Brace” contains two subdirectories, one called “new” and another call “ancient”. They contain different versions of the same set of source code files. We will use the diff program to see the differences between the two versions of “prog-1.c.”

diff brace/{new,old}/prog-1.c

If you have a standard directory skeleton that you need to create at the beginning of a project, you can quickly create them using key expansion. the mkdir -p option (dad) create missing parent directories when creating a child directory.

mkdir -p {source,build,man,help{/pages,/yelp,/images}}
tree

You can use the corset expansion with wget in order to download multiple files.

In this command, we are going to download files from two directories, called “test1” and “test2”. Each directory contains two files named “image1” e “image2”.

wget https://www.URL-of-your-choice.com/test{1,2}/picture{001,002}.jpg

List the files shows the files that were recovered and how wget rename files to avoid name conflicts with existing files.

ls picture*.*

Clamp the clamp

It seems that key expansion is another of Linux's best-kept secrets. Many people tell me that they have never heard of key expansion, while others inform me that it is one of their favorite command line tricks.

Give it a try and you might find your way around your set of command line tricks.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.