Linux string interception is generally used in shell scripts. In this article, I will write a few simple demos
to share with you.
First, define a variable
demo=https://blog.csdn.net/
-
1. Use#interception, delete the characters on the left, and keep the characters on the right
echo ${demo#*//}
. Among them,demois the variable name,#The sign is an operator,*//means the first from the left//number and all characters to the left, ie: deletehttp://
-
2. Use##interception, delete the characters on the left, and keep the characters on the right
echo ${demo##*/}
Note:##*/Indicates that the last (rightmost) / sign and all characters on the left are deleted from the left, namely: delete the entire
-
3. Use%interception, delete the characters on the right, keep the characters on the left
echo ${demo%/*}
Note:%/*means start from the right and delete the first/and the characters to the right
-
4. Use%%interception, delete the characters on the right, keep the characters on the left
echo ${demo%%/*}
Note:%%/*means start from the right and delete the last (leftmost) one/and the characters to the right
-
5. Intercept the number of characters starting from the left and the number of characters.
string="linux shell is very interesting"
echo ${string:0:${#string}-4}
Among them, 0 means starting from the first character on the left
-
6. The interception starts from the first few characters on the left and ends at the end
echo ${demo:6}
. Among them, 6 means starting from the 7th (6+1) character on the left and ending at the end.
-
7. Intercept the number of characters starting from the right and the number of characters.
echo ${demo:0-5:3}
Among them, 0-5 means counting from the right, starting from the 5th character, and 3 means the number of characters
-
8. The interception starts from the 6th character on the right and ends at the end
echo ${demo:0-6}
. Among them, 0-6 means starting from the 6th character on the right and ending at the end.
Note :
The first character on the left is used0Indicates that
the first character on the right is used0-1Express
Friends, have you lost your studies?
See you next time, bye!