Suppose there is a variable var=http://www.aaa.com/123.htm
1. # Intercept, delete the left character, leave the right character.
echo ${var#*//}
where Var is the variable name and the # is an operator, *//indicates 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.
echo ${var##*/}
##*/indicates that the last (rightmost) one/number and all characters to 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
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
echo ${var%/*}
%%/* indicates that the last (leftmost) one/number and right character are deleted from the right.
The result is: http:
5. Start with the first few characters on the left and the number of characters
echo ${var:0:5}
0 represents the first character on the left and 5 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.
echo ${var:7}
7 means that the 8th character on the left begins, 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
echo ${var:0-7:3}
0-7 means that the seventh character starts at the right, and 3 indicates the number of characters.
The result is: 123
8. Start with the first few characters on the right and continue to the end.
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 denoted by 0-1)
su226su226
136***[email protected]
Reference address
9 months ago (04-23)
Amau
[Email protected]***t
#!bin/bash#author: AmauVar= "http://www.runoob.com/linux/linux-shell-variable.html" s1 =${var%%*}s2=${ var%t*}s3 =${var%%.*}=${var#*/} s5=${var ##*/}
echo "about the intercept% of the string, #的使用方法"
echo "Original string:" ${var}
echo "%%t* effect:" ${s1}
echo "%t* effect:" ${s2}
echo "%%.* effect:" ${S3}
echo "#*/effect:" ${S4}
echo "##*/effect:" ${S5}
Operation Result:
About the truncation of the string, #的使用方法 the original string:http://www.runoob.com/linux/linux-shell-variable.html percentT * Effect:h%t* Effect:http://www.runoob.com/linux/linux-shell-variable.h%%.* Effect:http://www#*/effect:/www.runoob.com/linux/linux-shell-variable.html##*/ The effect: linux-shell-variable.html