${#string}
Returns the length of the $string
${string:position}
In $string, start extracting substrings from the $position position
${string:position:length}
In $string, the $length-length substring is extracted from the $position position
[Root@localhost shell]# name= "Ni hao, Ming tian"
[root@localhost shell]# echo $NAME
ni hao, Ming tian
[root@l Ocalhost shell]# Echo ${name}
ni hao, Ming tian
[root@localhost shell]# echo ${#NAME}
[ Root@localhost shell]# Echo ${name:3}
Hao, Ming Tian
[root@localhost shell]# Echo ${name:3:6}
Hao, M
[Root@localhost shell]#
${string#substring}
Delete the shortest match $substring substring starting at the beginning of the variable $string
${string# #substring}
Delete the longest matched $sunstring substring starting at the beginning of the variable $string
e
[root@localhost shell]# echo $NAME
abcabc123abcabcde
[root@localhost shell]# Echo ${name#a*c}
123ABCABCDE
[root@localhost shell]# echo ${name# #a *c}
ABCDE
${string%substring}
Delete the shortest match $substring substring starting at the end of the variable $string
${string%%substring}
Deletes the longest matched $substring substring starting at the end of the variable $string
[Root@localhost shell]# name=abcabc123abcabc
[root@localhost shell]# echo $NAME
abcabc123abcabc
[ Root@localhost shell]# Echo ${name%%a*c}
[Root@localhost shell]# Echo ${name%a*c}
abcabc123abc
[ Root@localhost shell]# Echo ${name#b*c}
abcabc123abcabc
[root@localhost shell]# Echo ${NAME#BC}
ABCABC123ABCABC
[Root@localhost shell]# Echo ${NAME%BC}
abcabc123abca
[root@localhost shell]# Echo ${ NAME%%BC}
abcabc123abca
[root@localhost shell]# Echo ${name%c*c}
abcabc123ab
[root@localhost shell]# Echo ${name%%c*c}
abcab
[root@localhost shell]# Echo ${name%%c*b}
abcabc123abcabc
[ Root@localhost shell]# Echo ${name}
abcabc123abcabc
Note: In the # or # #匹配时, the first character of the $string must be the first character of the deleted substring $substring, or the deletion cannot be matched;
in the case of a% or Percent match, the last character of the $string must be the last character of the deleted substring $substring, or the delete operation cannot be performed;
${parameter/parttern/string}
Replace the first matching pattern with a string
${parameter/#parttern/string}
Matches the pattern in the parameter variable from the beginning, and then uses string to replace the matching pattern.
${parameter/%pattern/string}
Matches the pattern in the parameter variable from the end, replacing the matching pattern with string after matching
${parameter//pattern/string}
Replace all matching pattern in the parameter variable with string
[Root@localhost shell]# name= "I am a student student"
[Root@localhost shell]# Echo ${name//student/teacher}
I am A teacher teacher
[root@localhost shell]# echo ${name/#student/teacher}
I am a student student
[ Root@localhost shell]# Echo ${name/%student/teacher}
I am a student teacher
[root@localhost shell]# Echo ${name/ %I Am/teacher}
I am a student student
[root@localhost shell]# echo ${name/#I am/teacher}
teachera Student Student
[root@localhost shell]# echo ${name/#I am A/teacher}
teacher student student
[root@localhost shell]# Echo $NAME
I am a student student
[Root@localhost shell]#