Common operation of variable quantum strings
${#string} Returns the length of the $string
${#string:p Osition} in $string, start extracting substrings from position $position
${#string:p Osition:length} in $string, extract the substring of length $length starting from the position $position
${string#substring} deletes the shortest match $substring substring starting at the beginning of the variable $string
${string# #substring} deletes the longest matching $substring substring starting at the beginning of the variable $string
${string%substring} deletes the shortest match $substring substring starting at the beginning of the variable $string open tail
${string%%substring} deletes the longest matching $substring substring starting at the beginning of the variable $string open tail
${string/ubstring/replace} from the variable $replace to replace the first matching $substring
${string/#substring/replace} if the $string prefix matches $substring, $replace is used instead of matching $substring
${string/%substring/replace} If the $string suffix matches $substring, use $replace instead of matching $substring
1. Returns the length of string Caimz variable string
[Email protected] shell_scrpit]# caimz= "I am a Boy"
[Email protected] shell_scrpit]# echo $caimz
I am a Boy
[Email protected] shell_scrpit]# echo ${#caimz} #查字符串的个数
10
[Email protected] shell_scrpit]# echo $caimz |wc-m
11
2, intercept Caimz variable string from the second character after the interception, the default to go back to all the characters, the second string is not included, also
Can be understood as deleting the preceding characters.
[[email protected] shell_scrpit]# echo ${caimz:2}
Am a Boy
3, intercept the CAIMZ variable string from the second string to start fetching, go to two characters
[[email protected] shell_scrpit]# echo ${caimz}
I am a Boy
[[email protected] shell_scrpit]# echo ${caimz:2:2}
Am
Or
[[email protected] shell_scrpit]# echo ${caimz} |cut-c 3-4
Am
4, starting from the beginning of the variable $caimz delete the shortest match "I am" substring
[[email protected] shell_scrpit]# echo ${caimz#i am}
A boy
5. Delete the longest match "I am a" substring starting from the beginning of the variable $caimz
[[email protected] shell_scrpit]# echo ${caimz# #I am A}
Boy
6. Delete the shortest matching boy string starting from the end of the variable $caimz
[[email protected] shell_scrpit]# echo ${caimz%boy}
I am A
7, starting from the end of the variable $caimz delete the shortest length matching boy string
[[email protected] shell_scrpit]# echo ${caimz%%boy}
I am A
8, use you is string to replace $caimz first match I am string
[[email protected] shell_scrpit]# echo ${caimz/boy/man}
I am a man
9. Use the man string to replace the boy string that starts at the end of the variable $caimz
[[email protected] shell_scrpit]# echo ${caimz/%boy/dog}
I am a man
10. Use the He is string instead of the I-AM string that starts at the beginning of the variable $caimz
[Email protected] shell_scrpit]# echo ${caimz/i am/you is}
You is a boy
Common operation of variable quantum strings