Linux There are eight ways to intercept a string.
Suppose there is a variable var=http://www.aaa.com/123.htm.
1. # Intercept, delete the left character, leave the right character.
The code is as follows:
Echo ${var#*//}
where var is the variable name and the# is an operator,*// means that the first // number and all characters on the left are deleted from the left .
Delete http ://
The result is:www.aaa.com/123.htm
2. # # Intercept, delete the left character and leave the right character.
The code is as follows:
Echo ${var##*/}
##*/ indicates that the last (rightmost) one / number and all characters on the left are deleted from the left.
That is, delete http://www.aaa.com/
The result is 123.htm .
3.% Intercept, delete right character, leave left character
The code is as follows:
Echo ${var%/*}
%/* indicates that the first / second and right characters are deleted from the right.
The result is:http://www.aaa.com
4. The percent number intercept, delete the right character, leave the left character
The code is as follows:
Echo ${var%%/*}
%%/* means to delete the last (leftmost) one / number and the right character from the right.
The result is:http:
5. start with the first few characters on the left and the number of characters
The code is as follows:
Echo ${var:0:5}
0 represents the first character on the left and5 indicates the total number of characters.
The result is:http:
6. start with the first few characters on the left and continue to the end.
The code is as follows:
Echo ${var:7}
7 of these means that the first 8 characters on the left begin, until the end.
The result is:www.aaa.com/123.htm
7. start with the first few characters on the right and the number of characters
The code is as follows:
Echo ${var:0-7:3}
The 0-7 means that the seventh character starts at the right, and3 indicates the number of characters.
The result is:123
8. start with the first few characters on the right and continue to the end.
The code is as follows:
Echo ${var:0-7}
The expression starts at the seventh character on the right and continues to the end.
Result is:123.htm
Note: (The first character on the left is denoted by 0 , and the first character on the right is represented by 0-1 )
A method of string interception by the shell