A string interception of Linux is useful. There are eight ways.
Suppose there is a variable var=http://www.linuxidc.com/123.htm
1 # Intercept, delete the left character, and 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.linuxidc.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.linuxidc.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.linuxidc.com
4 -percent 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 , until the end.
Echo ${var:7}
7 means that the 8th character on the left begins, until the end.
The result is:www.linuxidc.com/123.htm
7 starting with the first character 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, until 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)
A method of concatenation of strings in two Linux Shell scripts
If you want to add a character after a variable, you can use the following method:
$value 1=home
$value 2=${value1} "="
Echo $value 2
Add the string variable you want to add {}, and you need to put the $ out.
The result of this output is: home=, which means the connection was successful.
Another example:
[Email protected] sh]# var1=/etc/
[Email protected] sh]# var2=yum.repos.d/
[[email protected] sh]# var3=${var1}${var2}
[Email protected] sh]# echo $var 3
/etc/yum.repos.d/
Note: Reference connection: http://www.linuxidc.com/Linux/2015-03/115198.htm
Linux Shell string interception and splicing