1, the shell variable statement judgment
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 if its value is null, then the value is $default |
|
|
${var=default} |
If Var is not declared, then use $default as its value * |
${var:=default} |
If Var is not declared, or if its value is null, then the value is $default |
|
|
${var+other} |
If Var is declared, then its value is $other, otherwise it is a null string |
${var:+other} |
If Var is set, the value is $other, otherwise it is a null string |
|
|
${var? ERR_MSG} |
If Var is not declared, then print $err_msg * |
${var:? ERR_MSG} |
If Var is not set, then print $err_msg * |
|
|
${!varprefix*} |
Match all previously declared variables preceded by Varprefix |
${!varprefix@} |
Match all previously declared variables preceded by Varprefix |
* Very simple, we try to understand, not to say, note that there is no space between the dollar sign and the left curly braces, the left curly braces can not exist with the variable name spaces, variable names can not be in line with the judgement of the space between.
2, string operation (length gain, read, match delete, replace)
An expression |
meaning |
${#string} |
Length of $string |
|
|
${string:position} |
In $string, the substring is extracted from the position $position |
${string:position:length} |
In $string, a substring of length $length is extracted from the position $position. |
|
|
${string#substring} |
Deletes the substring of the shortest matching $substring from the beginning of the variable $string |
${string# #substring} |
Deletes the substring of the longest matching $substring from the beginning of the variable $string |
${string%substring} |
Deletes the substring of the shortest matching $substring from the end of the variable $string |
${string%%substring} |
Deletes 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 to replace all matching $substring |
${string/#substring/replacement} |
If the $string prefix matches the $substring, 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 |
|
|
* What needs to be explained is that substring can be regular expressions.
Alternative command: Cut sed awk, these three are more powerful string processing commands, capable of very many things about strings. Here are a few examples of this.
3, performance comparison
Time to I in $ (seq 10000);d o a=${#test};d one;
Time to I in $ (seq 10000);d o a=$ (expr length $test);d one;
Copy Code code as follows:
Real 0m0.181s
User 0m0.170s
SYS 0m0.000s
Real 0m8.580s
User 0m2.497s
SYS 0m6.075s
This is equivalent to looping through the external command processing strings such as awk, SED, cut, and length, which is time-consuming. There was a previous article about Shell optimization, where the shell loop is much slower than awk, where the performance test is not really meaningful, given the combination of the Shell optimization article.
4. String regular judgment
You can use the following commands like this:
Copy Code code as follows:
if [[] ${lastday? ERR_MSG} "=~" ^[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2}$ "]]
Then
...
Fi
It is said that it needs to be used in Bash version 3.0 to view the Bash version method: bash-version, view current interpreter kind command: Echo $