Get the length of the string
Shell Get string length is very simple, as follows:
${#string_name}
string_name string representing the name.
root@master:~# b="ma name is yjt" root@master:~# echo ${#b} 14
String interception
S Hell interception string usually in two ways: Start and taken from the specified character (substring) taken from a specified location
Taken from the specified start location
Counting from the left of the string:
If you want to start counting from the left of the string, then the string is taken as the specific format:
${string: start :length}
Here, string is the string to be intercepted, start is the starting position (starting from the left, starts counting from 0), the length is taken to be the length (not until then represents the end of the string).
Example:
root@master:~# echo ${b:3:4} name root@master:~# echo ${b:3} name is yjt
Counting from the right string:
If you want to start counting from the right of the string, then the string is taken as the specific format:
${string: 0-start :length}
Compared with the first) formats, 2) more formats only 0-
, which is fixed wording, designed to indicate the start counting from the right string.
It should be emphasized two points:
- When counted from the left, starting number is 0 (This is consistent thinking the programmer); the right from the beginning when the count start number is 1 (This is consistent with ordinary thinking). Different counting directions, starting number are different.
- Regardless of which side starts counting directions are taken from left to right.
Example:
root@master:~# echo ${b:0-11:4} name root@master:~# echo ${b:0-11} name is yjt
From the specified character or character begins intercepting child
This method can not intercept length of the string, can be taken from the specified character (a character string) to the end of the string. Shell can intercept specified character (substring) of all characters to the right, you can also intercept all the characters to the left.
Use the # character to the right of interception
Use #
numbers can intercept the specified character (or substring) of all characters to the right, the following format:
${string#*chars}
Here, string representing the character to be intercepted, chars are specified character (or sub-string) *
is a wildcard, represents an arbitrary length string. *chars
To link the use of means: ignore all characters to the left, until I met chars (chars will not be intercepted).
root@master:~# url="http://www.baidu.com/tomcat" root@master:~# echo ${url#*:} //www.baidu.com/tomcat
The three inside url / matching / results are as follows:
root@master:~# echo ${url#*/}
/www.baidu.com/tomcat
If you want to get tomcat or match the last / it?
If you want to last until a specified character (substring) and then match the end, you can use ##
the specific format:
${string##*chars}
root@master:~# echo ${url##*/}
tomcat
Use the% character left interception
Use %
numbers can intercept the specified character (or substring) all the characters to the left, the following format:
${string%chars*}
Please note *
the location, because the character chars left to be intercepted, and the character to ignore the right of the chars, it *
should be located on the right side of chars. Other aspects %
and #
the use of the same, is not repeated here, way of example only:
Master @ the root: ~ # echo URL% $ { / * } // matched from the rightmost /. http://www.baidu.com the root Master @: ~ # echo URL %% $ {/ *} HTTP: