Linux-{curly brace} in bash

Linux curly brace: “{}”
Linux: curly braces bash

Linux brackets or braces:

1
2
3
- curly brace:{}
- curvy brace:()
- straight brace:[]

Linux quotes:

1
2
3
- quotes: ''
- double-quotes: ""
- backquotes: ``

1. Array Builder

Here braces {} being used as apart of a sequence builder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
$ echo {0..10}
0 1 2 3 4 5 6 7 8 9 10

$ echo {10..0}
10 9 8 7 6 5 4 3 2 1 0

$ echo {0..5}{0..5}
00 01 02 03 04 05 10 11 12 13 14 15 20 21 22 23 24 25 30 31 32 33 34 35 40 41 42 43 44 45 50 51 52 53 54 55

$ for i in {0..5};do echo $i;done
0
1
2
3
4
5

$ for i in {0..10..2};do echo $i;done
0
2
4
6
8
10

$ for ch in {a..f};do echo $ch;done
a
b
c
d
e
f

$ for ch in {z..a..3};do echo $ch;done
z
w
t
q
n
k
h
e
b

#创建260个文件
$ touch file{a..z}{0..9}


2. Interpreting a variable as its value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#brace {} being used as a way of generating parameter expansion
$ month=("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
$ for i in {0..12};do echo ${month[i]};done
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec

3. Parameter expansion

1
2
3
4
5
6
7
8
9
10
11
$ a="Too longgg"

#切掉某字符串, 保留它前面的
$ echo ${a%gg}

#convert jpg file to png file
$ i=image.jpg
$ mv $i ${i%jpg}png #image.jpg -> image.png

#切掉某字符串,保留它后面的
$ mv $i quantux${i#image}.jpg #image.jpg -> quantux.jpg

4. Output grouping

Group the outputs of all these commands together.

1
2
3
4
5
6
7
8
9
10
11
$ echo "c++"; echo "c language"; echo "Java";echo "Golang" > out.txt
$ cat out.txt
Golang

# "{ "后面必须有空格, " }"前面必须有空格, 最后一个命令必须有";"
$ { echo "c++"; echo "c language"; echo "Java";echo "Golang"; } > out.txt
$ cat out.txt
c++
c language
Java
Golang