Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the curly braces extensions in bash?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about the expansion of curly braces in bash. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

All the expansion of bash are as follows:

Brace Expansion (curly braces extension)

Tilde Expansion (tilde extension)

Parameter and Variable Expansion (parameter and variable extension)

Command Substitution (command replacement)

Arithmetic Expansion (arithmetic extension)

Word Splitting (word segmentation)

Pathname Expansion (path extension)

The order listed above is exactly the order in which bash is extended.

Definition of curly braces expansion (Brace Expansion)

Curly braces extension, also known as curly braces extension, is an extension that allows bash to generate arbitrary strings. It is very similar to path extension, except that the generated string can be a path or file name that does not exist.

In bash, curly braces extension has the highest priority among many extensions, so a statement similar to echo {acentine b} $PATH should result in a$PATH b$PATH after completing the curly brace extension, while the extension of PATH environment variables does not begin until the subsequent "parameter and variable extension" phase.

Two formats of curly braces extension

3.1 the first type of format is:

Preamble+ {string1,string2,string3,...,stringN} + postcript

Left and right curly braces are required, and the middle string list is separated by commas. Note that there can be no spaces before and after commas. If there are spaces in string, single or double quotation marks are required.

When bash actually extends, it concatenates preamble with all the strings in curly braces (from left to right), and finally adds postscript.

In addition, there is at least one comma in the middle of the curly braces, otherwise bash would not consider the curly braces extensions, for example:

Echo {moneny}. If you want to output money, change it to echo {money,} as follows:

[root@master01 ~] # echo {money}

{money}

[root@master01 ~] # echo {money,}

Money

Check out the complete example (pay attention to the handling of spaces)

[root@master01 ~] # echo sp {el,il,al} l

Spell spill spall

[root@master01 ~] # echo sp {el,il, al} l

Sp {el,il, al} l

[root@master01 ~] # echo sp {el,il,' al'} l

Spell spill sp all

[root@master01 ~] # echo sp {el,il, "" al} l

Spell spill sp all

[root@master01 ~] # echo sp {el,il,''al} l

Spell spill sp all

[root@master01 ~] # echo sp {el,il, "al"} l

Spell spill sp all

3.2 the second type of format is:

Preamble+ {.... [INCR]} + postscript

Among them; among them. The combined expression term is called a sequence expression (sequence expansion), which represents a specific range. When the sum is a number, it represents a range of numbers; when the sum is a single letter, it represents a range of characters (the default LC_ALL character arrangement). And must be numeric or alphabetic, otherwise bash does not think of curly braces as an extension, but output as is.

[root@master01 ~] # echo {1.. 12}

12 3 4 5 6 7 8 9 10 11 12

[root@master01 ~] # echo {3... Mustang 2}

3 2 1 0 1 2

[root@master01 ~] # echo {a.. g}

A b c d e f g

[root@master01 ~] # echo {h.. a}

H g f e d c b a

One of them is optional and represents the increment of the interval range, which must be a number. For example:

[root@master01 ~] # echo {0.10. 2}

0 2 4 6 8 10

Starting at 0, the corresponding number is taken out for every 2 increments.

If it is not specified, then the default is 1 or-1, specifically 1 or-1, depending on whether the range of the previous range is increasing or decreasing. For example, in the example {a. G} above, the default is 1, and {h. A} defaults to-1.

In addition, when the sum is a number, we can keep the output length consistent by adding 0 before the number.

[root@master01 ~] # echo {1.. 10}

1 2 3 4 5 6 7 8 9 10

[root@master01 ~] # echo {01.. 10}

01 02 03 04 05 06 07 08 09 10

[root@master01 ~] # echo {001.. 10}

001 002 003 004 005 006 007 008 009 010

If the format is the same length, it will look beautiful.

This is what the curly braces extension in bash is like. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report