伯恩命令解释器常识。Bash 101
command expansions
Simple expansion
- Tilde expansion:
echo ~
yields/home/dev
- Parameter expansion:
echo $winIp
yields172.18.112.1
Arithmetic expansion
Bash shell does not natively support floating-point arithmetic. However, you can use the bc
command to perform floating-point arithmetic in Bash. bc
is an arbitrary precision calculator language that can handle floating-point numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ echo $(( 2 + 2 ))
4
$ echo $(( 2 * 3 ))
6
$ echo $(( 10 / 2 ))
5
$ echo $(( 10 % 3 ))
1
$ echo $(( 2 ** 3 ))
8
$ echo "scale=4; 22/7" | bc
3.1428
$ echo "scale=7; 333/106" | bc
3.1415094
In the above example, we used the $(( ))
syntax to perform integer arithmetic expansion. The scale variable is used to specify the number of decimal places to display in the result. In this case, we set it to 2 to display two decimal places. The |
symbol (vertical line i.e. pipe symbol) is used to pipe the output of the echo command to bc.
Pathname expansion
Pathname expansion is used to generate a list of filenames that match a pattern. Here are some examples of pathname expansion:
echo *
yields all files in the current directoryecho *.txt
yields all files in the current directory that end with .txtecho /usr/*/share
yields all directories in/usr
that have a subdirectory calledshare
echo ./test/input-*.txt
yields allinput-1.txt
,input-2.txt
,input-999.txt
.echo "*.dat*
yields all file and directory names that contain.dat
.
Grouped brace expansion is also supported.For example, if you have a large collection of images you want to organize into years and months, you can create a series of directories named in numeric “Year-Month” format. This way, the directory names will sort in chronological order. Instead of typing out a complete list of directories, you can use pathname expansion to create them quickly and easily. Here’s an example:
Brace expansion
echo chapter{1..3}
yieldschapter1 chapter2 chapter3
.echo {A..Z..2}
yieldsA C E G I K M O Q S U W Y
.echo {a..c}" "{1..3}
yieldsa 1 a 2 a 3 b 1 b 2 b 3 c 1 c 2 c 3
. Note that blank space is quoted.echo {a..c}{x,y}{1..3}
yieldsax1 ax2 ax3 ay1 ay2 ay3 bx1 bx2 bx3 by1 by2 by3 cx1 cx2 cx3 cy1 cy2 cy3
.
Pathname expansion is commonly used to create lists of files or directories to be created. For example, if you have a large collection of images you want to organize into years and months, you can create a series of directories named in numeric “Year-Month” format. This way, the directory names will sort in chronological order. Instead of typing out a complete list of directories, you can use pathname expansion to create them quickly and easily. Here’s an example:
1
2
3
4
5
6
7
$ mkdir Photos
$ cd Photos
$ mkdir {2017..2019}-{01..12}
$ echo {2017..2019}-{01..12}
2017-01 2017-02 2017-03 2017-04 2017-05 2017-06 2017-07 2017-08 2017-09 2017-10 2017-11 2017-12
2018-01 2018-02 2018-03 2018-04 2018-05 2018-06 2018-07 2018-08 2018-09 2018-10 2018-11 2018-12
2019-01 2019-02 2019-03 2019-04 2019-05 2019-06 2019-07 2019-08 2019-09 2019-10 2019-11 2019-12
This will create directories named 2017-01, 2017-02, 2017-03, and so on, up to 2019-12.
Command substitution
Command substitution allows you to use the output of a command as an argument to another command. For example, echo $(date)
will print the current date and time.
1
2
$ echo "The current date is `date +%Y-%m-%d`"
The current date is 2023-03-19