String manipulation:
String slice: ${var:offset:length}
A= "World"
The result of ${a:2:2} is: RL
Take the last few characters of the string: ${var:-length}
Note: There are spaces after the colon
To take a substring based on a pattern:
${var#*word}: Where word can be any character specified, from left to right, Finds the first occurrence of word in the stored character of the Var variable, deleting all characters from the beginning of the character until the first occurrence of the Wrod;
# mypath= ' Sysconfig/network-scripts/ifcfg-eth0 '
# echo ${mypath#*/}
Network-scripts/ifcfg-eth0
${var##*word}: Where word can be any character specified, from left to right, look for the character stored in the Var variable, the last occurrence of word, Deletes all characters from the beginning of the character until the last occurrence of the Wrod;
# mypath= '/ Sysconfig/network-scripts/ifcfg-eth0 '
# Echo ${mypath##*/}
ifcfg-eth0
${var%word*}: From right to left, delete the first word that appears at the beginning of the character until all the characters in the tail;
${var%%word*}: From right to left, delete all characters at the beginning of the last word occurrence until the end of the character;
Example: url=http://www.magedu.com:80
Fetch port: ${url##*:}: Removes all characters before the last colon, which is the string after the last colon
Fetch protocol: ${url%%:*}: From right to left, delete the character after the last colon, that is, from right to left, take the string that precedes the last colon
Find and Replace:
${var/pattern/substi}: Find the string represented by Var, the first string to be matched by pattern, and replace it with Substi;
${var//patten/substi}: Find the string represented by Var, all the strings that are matched by pattern, and replace them with Substi;
${var/#pattern/substi}: The pattern is anchored at the beginning of the line to match the patterns to the string represented by Var, and if it matches, it is replaced with substi;
${var/%pattern/substi}: The pattern is matched to the string represented by Var at the end of the line, and replaced by substi if it matches;
can use?, * meta-characters;
Find and Delete:
${var/pattern}: Delete pattern match to the first occurrence;
${var//pattern}: Delete all occurrences of pattern matching;
${var/#pattern}
${var/%pattern}
String Case Conversion:
${var^^}: Lowercase-to-uppercase
${var,,}: uppercase-to-lowercase
lowercase to uppercase: echo "abc" | TR ' [A-z] ' [A-z] '
Uppercase to lowercase: echo "ABC" | TR ' [A-z] ' [A-z] '
Refer to: http://blog.csdn.net/linwhwylb/article/details/5904584
Variable assignment:
${var:-word}: Returns word if Var is empty or not set, otherwise returns the value in Var;
${var:=word}: Returns word if Var is empty or not set, and assigns Word to Var; otherwise, returns the value in Var;
${var:?err_info}: Returns an error message if Var is empty or not set, otherwise returns the value of Var itself;
${var:+word}: Returns word if VAR has its own normal data;
Trap command:
Trap ' arg ' SIGNAL
For example: Trap ' echo ' quit '; Exit 1 ' SIGNAL #不会被脚本中的命令接收到 will only be received by the script itself
The above is their own summary, can also refer to:
http://blog.csdn.net/horkychen/article/details/7676151
Bash string manipulation