Thanks to the original, the article is very helpful.
Transferred from: http://www.cnblogs.com/chengmo/archive/2010/10/02/1841355.html
First, Judge read the string value
An expression |
meaning |
${var} |
Variable var with the same value as $var |
|
|
${var-default} |
If Var is not declared, then use $default as its value * |
${var:-default} |
If Var is not declared, or its value is empty, then $default is used as its value * |
|
|
${var=default} |
If Var is not declared, then use $default as its value * |
${var:=default} |
If Var is not declared, or its value is empty, then $default is used as its value * |
|
|
${var+other} |
If Var is declared, then its value is $other, otherwise it is a null string |
${var:+other} |
If Var is set, then its value is $other, otherwise it will be a null string |
|
|
${var? ERR_MSG} |
If Var is not declared, print $err_msg * |
${var:? ERR_MSG} |
If Var is not set, then print $err_msg * |
|
|
${!varprefix*} |
Matches all previously declared variables beginning with Varprefix |
${[email protected]} |
Matches all previously declared variables beginning with Varprefix |
Adding "*" does not mean: Of course, if the variable var is already set, then its value is $var.
[[email protected] ~]$ echo ${abc-' OK '}
Ok
[Email protected] ~]$ echo $ABC
[[email protected] ~]$ echo ${abc= ' OK '}
Ok
[Email protected] ~]$ echo $ABC
Ok
If ABC does not declare "=" It will also assign a value to ABC.
[Email protected] ~]$ var1=11;var2=12;var3=
[[email protected] ~]$ echo ${[email protected]}
Var1 var2 Var3
[[email protected] ~]$ echo ${!v*}
Var1 var2 Var3
${!varprefix*} similar to ${[email protected]}, a variable name prefix character can be used to search for a variable that has already been defined, regardless of whether it is a null value.
Second, string operation (length, read, replace)
An expression |
meaning |
${#string} |
Length of $string |
|
|
${string:position} |
In $string, start extracting substrings from position $position |
${string:position:length} |
In $string, a substring of length $length is extracted starting from position $position |
|
|
${string#substring} |
Delete the substring of the shortest match $substring from the beginning of the variable $string |
${string# #substring} |
Delete the substring of the longest matching $substring from the beginning of the variable $string |
${string%substring} |
Delete the substring of the shortest match $substring from the end of the variable $string |
${string%%substring} |
Delete the substring of the longest matching $substring from the end of the variable $string |
|
|
${string/substring/replacement} |
Use $replacement to replace the first matching $substring |
${string//substring/replacement} |
Use $replacement instead of all matching $substring |
${string/#substring/replacement} |
If the $string prefix matches the $substring, then the $replacement is used instead of the matching $substring |
${string/%substring/replacement} |
If the $string suffix matches the $substring, then the $replacement is used instead of the matching $substring |
|
|
Description: "* $substring" can be a regular expression .
1. Length
[Email protected] ~]$ test= ' I love China '
[Email protected] ~]$ echo ${#test}
12
${#变量名} to get the string length
2. Intercept strings
[Email protected] ~]$ test= ' I love China '
[[email protected] ~]$ echo ${test:5}
E China
[[email protected] ~]$ echo ${test:5:10}
E China
${variable Name: start: length} get substring
3. String deletion
[Email protected] ~]$ test= ' C:/windows/boot.ini '
[[email protected] ~]$ echo ${test#/}
C:/windows/boot.ini
[[email protected] ~]$ echo ${test#*/}
Windows/boot.ini
[[email protected] ~]$ echo ${test##*/}
Boot. ini
[[email protected] ~]$ echo ${test%/*}
C:/windows
[[email protected] ~]$ echo ${test%%/*}
${variable name #substring regular expression} starts with substring from the beginning of the string and deletes the expression on the match.
${variable name%substring regular expression} starts with substring from the end of the string and deletes the expression on the match.
Note: ${test##*/},${test%/*} is the simplest way to get the file name, or directory address, respectively.
4. String substitution
[Email protected] ~]$ test= ' C:/windows/boot.ini '
[[email protected] ~]$ echo ${test/\//\\}
C:\windows/boot.ini
[[email protected] ~]$ echo ${test//\//\\}
C:\windows\boot.ini
${variable/find/Replace value} A "/" means replacing the first, "//" means replacing all, when the lookup appears: "/" Please add escape character "/" to indicate.
Third, performance comparison
In the shell, through awk,sed,expr and so on can be implemented, the string above operation. Below we perform a performance comparison.
[Email protected] ~]$ test= ' C:/windows/boot.ini '
[[Email protected] ~]$ time for I in $ (seq 10000);d o a=${#test};d one;
Real 0m0.173s
User 0m0.139s
SYS 0m0.004s
[[Email protected] ~]$ time for I in $ (seq 10000);d o a=$ (length of expr $test);d one;
Real 0m9.734s
User 0m1.628s
The speed difference is hundreds of times, call external command processing, and built-in operator performance is very different. In shell programming, try to complete with built-in operators or functions. Similar results can occur with awk,sed.
Linux shell String Operations (length, find, replace)