How to use the braces extension in Linux Bash Shell – Guide

If you’re an advanced Linux user, you’ve probably done a lot of work with the command shell, both the command-line interface (CLI) and shell scripts. The Linux command line offers a lot of flexibility and powerful options when working with various commands: regular expressions, wildcards, metacharacters, operators, file and I/O redirection, brace expansion, etc. We will see How to use brace expansion to perform various actions by combining different file and directory names or with any command argument. Understanding brace expansion, which uses braces ({}), will make it easier to run many commands from multiple files.

strap extension

Before the Bash shell executes a command in a terminal window or a line in a script, it checks to see if it needs to do any substitutions in the command. Variable names are replaced with their values, aliases are replaced with commands for which they are abbreviated, and any expansion is performed. Brace expansion is a form of expansion supported by Bash. The brace expansion is available in modern shells, but may be missing in some older shells. If you plan to use brace expansion in scripts, be sure to call a shell that supports brace expansion, such as Bash.

We’ll use Bash for our examples.

Generating channel lists might seem more of a novelty than a benefit, but it does offer some features which can save time and keystrokes. It can often provide a simple and elegant solution to a problem or requirement.

simple extensions

A brace expansion is contained between a pair of braces “{}”. This can be a comma-separated list of items or a range specifier. Spaces are not allowed between curly braces unless you enclose the string in quotes ” “. For a comma-separated list, the expansion process takes each item in turn and passes it to the calling command. In this example, it is echo which simply prints them to the terminal window. Note that commas are ignored. A list can be made up in words or numbers. The order of items in the list is completely arbitrary. An extension range has a leading character and a final linked by colons ” .. »Without any spaces. Any missing list items are provided automatically by expansion so that the entire range from the start character to the final be created. This will print the numbers 1 to 10. The numbering is arbitrary. You don’t have to start with one. Intervals can be specified to run backwards. This will generate a five to one list. Ranges can include negative numbers. As we pointed out earlier, a range has a start character and a final. It doesn’t have to be a number. It could be a letter. Letters can also be inverted.

Using the curly strap extension

You can use ranged brace expansion inside loops in scripts. Bracket expansion ranges allow you to use characters as a loop variable. Loops are commonly used in scripts, but there’s nothing stopping you from typing them on the command line to see what happens.

Concatenation and nesting

Two adjacent extensions do not act independently one after the other. They interoperate. Each element of the first expansion is subject to the action of each element of the second expansion. for i in {m..q}; do echo $i; done Extensions can also be nested. A nested expansion will act on the element immediately preceding it. You can also nest extents by creating a comma-delimited list of range extents.

Preamble and Postscript

You can enclose text before and after an expansion in braces to include that text in the expansion results. Text placed in front of an extension is called a preamble, while text placed behind an extension of curly braces is called postscript. This command uses a preamble. This example uses a postscript: And this command uses both.

Extension of file and directory names

As you’ve probably guessed, one of the main uses for key extensions is to create file and directory names that can be passed to other commands. We use echo as a convenient way to see exactly what happens when an extension is triggered. You can substitute any command that takes filenames or directory names as input and uses brace expansion with it. To create some files quickly, use touch. 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 extensions can help. Here, we are zipping a subset of files with the base name “program” into a ZIP file called “source”. ls * .txt Development directories contain many files that will have the same base name as your main program. Usually you don’t want to go back up or distribute files as “.o” object files. This is a great way to only include the file types that interest you. This command will make a copy of a file and append “.bak” to it, making a backup copy of the original file. An interesting point to note is that the brace expansion contains a comma-separated list, but the first element is empty. If we hadn’t included the comma, the expansion would not have taken place. To perform an action on two files in different directories, we can use a key extension in the file path. In this example, the “keys” directory contains two subdirectories, one called “new” and one called “old”. They contain different versions of the same set of source files. We will use the diff program to see the differences between the two versions of “prog-1.c”. ls key/new/prog-1.c.bak If you have a standard skeleton of directories that you need to create at the beginning of a project, you can quickly create them using brace expansion. the mkdir -p option The (parent) option creates any missing parent directories when creating a child directory. You can use key expansion with wget to download multiple files. In this command, we are going to download files from two directories, named “test1” and “test2”. Each directory contains two files named “image1” and “image2”. tree The file list shows which files were recovered and how wget renames files to avoid name conflicts with existing files.

Final note

I hope you like the guide How to use the braces extension in Linux Bash Shell. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.