One, the string slice:
${#var}: Returns the length of the string variable var
${var:offset}: Returns the string variable var starting with the character specifier (excluding the first offset character) from the first, to the last part, where the value of offset is between 0 and ${#var}-1 (after bash4.2, negative values are allowed)
${var:offset:number}: Returns the string variable var starting with the character specifier (excluding the first offset character) from the first, the part of the length number
${var:-length}: Takes the rightmost few characters of a string, note that there must be a white space character after the colon
${var:offset:-length}: Skips the offset character from the leftmost side, always to the right before the Lengh character from the rightmost
${var:-length:-offset}: First from the right to the left to the length of the beginning, and then to the right to the right side of the content between the offset characters, note:-length Before the space, the same expression, offset performance behavior and the array is reversed.
Example of a string slice:
- A=' abcdefghijklmn '
- echo ${#a} #输出字符串a的总长度
- 14
- echo ${A:4} #输出字符串a从0开始数, containing all elements after the 4th element
- Efghijklmn
- echo ${A:4:3} #输出字符串a从0开始数, containing 3 elements after the 4th element
- Efg
- echo ${A:-4} #从字符串a的右侧向左取4个元素, must have a null character after the colon
- Klmn
- echo ${A:5:-2} #输出字符串a从0开始数, contains the 5th element of the right number of I to the bottom 3 elements, note that this usage depends on the bash version, try not to use.
- Fghijkl
- echo ${A:-5:-2} #从字符串a的右侧向左数5个, then to the right to the last 3 characters, note that this usage depends on the bash version, try not to use it.
- Jkl
- echo ${A:-5:2} #从字符串a的右侧向左数5个, and then 2 characters to the right
- Jk
Second, based on the pattern to take the substring
${var#*word}: Where word can be specified by any character function: From left to right, find the var variable stored in the string, the first occurrence of Word, delete all characters from the beginning of the string to the first occurrence of Word characters
${var##*word}: Ditto, greedy mode, different, delete is all the content between the beginning of the string and the last character specified by word
${var%word*}: Where word can be any character specified; function: From right to left, find the var variable stored in the string, the first occurrence of word, delete the last character of the string to the left to the first occurrence of all characters between word characters;
${var%%word*}: Ditto, except delete all characters from the rightmost character of the string to the left to the last occurrence of word characters;
Character Processing instances:
- a= Abcdefg_a_ Fghijklmn '
- Echo ${ a#*FG} # From the left, delete the start position to the first FG, including FG
- _a_fghijklmn
- Echo ${ a##*FG} #贪婪模式, left to delete, remove start position to last FG, including FG
- hijklmn
- Echo ${ a%fg*} #从右边开始删, delete the end position to the first FG, including FG
- abcdefg_a_
- Echo $< Span class= "Br0" >{ a%%fg*} #贪婪模式, delete from the right, remove the focus position to the last FG, including FG
- abcde
Third, find and replace the string
${VAR/PATTERN/SUBSTR}: Finds the string that is represented by Var, the first time it is matched to by pattern, and replaces it with SUBSTR
${VAR//PATTERN/SUBSTR}: Find the string represented by Var, all strings that can be matched to by pattern, and replace it with SUBSTR
${var/#pattern/substr}: Finds the string that is represented by Var in the string that the beginning of the line is matched to by pattern, in order to substr replace the
${VAR/%PATTERN/SUBSTR}: Finds the string in the string represented by Var, where the end of the line is matched by pattern, to substr replace the
To find a replacement instance:
- A=' abcdefg_a_fghijklmn '
- echo ${a/fg/dd} #替换从左边数第一个fg为dd
- Abcdedd_a_fghijklmn
- echo ${a//fg/dd} #替换字符串中所有的fg为dd
- Abcdedd_a_ddhijklmn
- echo ${/#ab/dd} #如果开头是ab则替换成dd
- Ddcdefg_a_fghijklmn
- echo ${/#fd/dd} #如果开头是fd则替换成dd
- ABCDEFG_A_FGHIJKLMN #开头不是fd不做替换
- echo ${a/%mn/dd} #结尾是mn则替换成dd
- Abcdefg_a_fghijkldd
- echo ${a/%fd/dd} #如果结尾是fd则替换成dd
- ABCDEFG_A_FGHIJKLMN #结尾并不是fd不做替换
Iv. Finding and deleting strings
Similar to find substitution, except that there are no replacement values
${var/pattern}: Delete the string in the string represented by Var for the first time that the pattern is matched to
${var//pattern}: Removes all strings that are matched by pattern in the string represented by Var
${var/#pattern}: Removes all strings in the string represented by Var that match the beginning of pattern
${var/%pattern}: Removes all strings in the string represented by Var that match pattern to the end of the line
As with the find replacement syntax, there is no substitute value or slash, and this is not a demonstration.
Five, character case conversion
${var^^}: Converts all lowercase letters in VAR to uppercase
${var,}: Converts all uppercase letters in VAR to lowercase
Summary: The use of string processing inside the shell can improve a lot of productivity and save some of the machine performance, but note that the value of the string processing variable does not change the data of the variable, but only when the output is changed.
Vi. assigning values to string variables
Assigning a value to a variable using a string can replace the partial if else to make the syntax more concise.
Shell string Processing