substring interception
Method 1: ${variable name: starting position: length}
Method 2: expr subsrt "$variable name" starting position length
Method 3: echo $variable name | cut -b start position - end position
[root@server0 ~]# phone='13788768897'
[root@server0 ~]# echo ${#phone} Display the number of characters in the string
11
[root@server0 ~]# echo ${phone:0:6 } The intercepted string starts from 0 and the 6 from left to right is the number
137887
[root@server0 ~]# echo $phone | cut -b 1-4 intercepts the string from 1 to 4 of the string
1378
[root@ server0 ~]# expr substr "$phone" 1 3 intercepts the string from 1 to 3
137
[root@server0 ~]# echo $phone | cut -b 1,4,8 intercepts the string from 1, Character
188 in position 4, 8
syntax format:
${variable name/old/new} only replaces the first matching result
${variable name/old/new} replaces all matching results
Example:
[root@server0 ~]# phone='13788768897'
[root@server0 ~]# echo ${phone/3/@}
1@788768897
[root@server0 ~]# echo ${phone//8/*}
137**76**97
Pick the head to the tail according to the condition
: from left to right, the shortest match deletes
${variable name#*keyword}
from left to right, the longest match deletes
${variable name##*keyword}
to the end:
from right Left, shortest match delete
${variable name#*keyword}
From right to left, the longest match is deleted
[root@server0 ~]# phone='13788768897'
[root@server0 ~]# echo ${phone#*8}
8768897
[root@server0 ~]# echo ${phone##*8}
97
[root@server0 ~]# echo ${phone%8*}
13788768
[root@server0 ~]# echo ${phone%%8*}
137