First, string slices:
${var:offset:number} takes a substring of a string;
Take the rightmost few characters of the character toe: ${var:-length}
Note: There must be a white space character after the colon;
Second, To take a substring based on a pattern:
1.${var#*word}
Word is the specified delimiter;
From left to right, look for the first occurrence of the word delimiter in the string stored by the var variable, and delete all characters from the beginning of the string to the delimiter
2.${var##*word}
From left to right, look for the last occurrence of the word delimiter in the string stored by the var variable, removing the string from the beginning of all the characters between the delimiters
3.${var%word*}
From right to left, look for the first occurrence of the word delimiter in the string stored by the var variable, removing the delimiter to all characters between the trailing end of the string
4.${var%%word*}
From right to left, look for the last word delimiter that appears in the string stored by the var variable, removing the delimiter to all characters between the trailing end of the string
Example:
Mypath= "/etc/init.d/functions"
${mypath##*/}: Functions
${mypath#*/}: Etc/init.d/functions
${mypath%/*}:/ETC/INIT.D
Url=http://www.magedu.com:80
${url##*:}
${url%%:*}
Third, Find Replacements
Note: Use Glob style and wildcard characters in pattern;
(1) ${var/pattern/substi}
Finds the string in the string represented by Var that was first matched to by pattern, replacing it with the string represented by Substi
(2) ${var//pattern/substi}
Finds all strings that are matched by pattern in the string represented by Var and replaces them all with the string represented by Substi
(3) ${var/#PATTERN/substi}
Finds the string represented by Var, and replaces it with the string represented by Substi by the string to which the beginning of the line is matched by the pattern;
(4) ${var/%pattern/substi}
Find the string represented by Var, and replace it with the string represented by Substi by the string that the end of the line is matched to by the pattern;
Four, Find Delete:
${var/pattern}: Finds the first match in the Var string in pattern mode and deletes it;
${var//patern}
${var/#PATTERN}
${var/%pattern}
Five, character-case conversions:
${var^^}: Converts all lowercase characters in var to uppercase;
${var,}: Converts all uppercase characters in var to lowercase;
Six, variable Assignment:
${var:-value}: Returns value if the var variable is empty or not set, otherwise returns the var variable;
${var:=value}: If the var variable is empty or not set, then return value and assign value to the var variable, otherwise return the value of the Var variable;
${var:+value}: Returns VALUE if the var variable is not empty;
${var:? Error_info}: If Var is empty, or not set, then return error_info as error; otherwise, return var value;
This article is from the "Xu Ding blog" blog, make sure to keep this source http://xuding.blog.51cto.com/4890434/1740550
Shell Script Programming Overview (IV): Bash built-in character processing tool