Bash Brace Expansion
(this post if part of the material I cover in my devops course)
About Brace Expansions
This post is based on the documentation of bash brace expansion.
- Brace expansion is a mechanism that creates strings
- The basic syntax is:
- optional preanble (an opening string)
- a series of comma-separated strings or
- a sequence expression between a pair of braces
- optional postscript (a closing string)
- optional preanble (an opening string)
- The preamble is prefixed to each string contained within the braces
- The postscript is then appended to each resulting string, expanding left to right.
- Here's a basic example:
1$>
2$> printf "%s\n" A{1,2,3,4}B
3A1B
4A2B
5A3B
6A4B
7$>
8$> for word in A{1,2,3,4}B
9> do
10> printf "%s\n" $word
11> done
12A1B
13A2B
14A3B
15A4B
16$>
Sequence Brace Expansion
- A sequence expression takes the form {x..y[..incr]}, where:
- x and y are either integers or letters
- incr, an optional increment, is an integer.
- Example:
1$> printf "%s\n" A{0..30..3}B
2A0B
3A3B
4A6B
5A9B
6A12B
7A15B
8A18B
9A21B
10A24B
11A27B
12A30B
13$>
- example with letters:
1$> printf "%s\n" +{a..z..4}-
2+a-
3+e-
4+i-
5+m-
6+q-
7+u-
8+y-
9$>
Nested Brace Expansions
Just an example:
1$> printf "%s\n" +{{A..E},f,g,{H..K}}+
2+A+
3+B+
4+C+
5+D+
6+E+
7+f+
8+g+
9+H+
10+I+
11+J+
12+K+
13$>