String-related operations are often involved in shell batch processing. There are many command statements, such as awk and sed, which can perform various string operations. In fact, shell has a series of built-in operation symbols, which can achieve similar results. as you know, using internal operators will omit starting external programs...
String-related operations are often involved in shell batch processing.
There are many command statements, such as awk and sed, which can perform various string operations.
In fact, shell has a series of built-in operation symbols, which can achieve similar results,
As you know, using internal operators will omit the wait time for external programs to start,
Therefore, the speed will be very fast.
I. judge to read the string value
Expression meaning
The value of the variable var $ {var} is the same as that of $ var.
$ {Var-DEFAULT} If var is not declared, use $ DEFAULT as its value.
$ {Var:-DEFAULT} If var is not declared or its value is null, $ DEFAULT is used as its value.
$ {Var = DEAULT} If var is not declared, use $ DEFAULT as its value.
$ {Var: = DEFAULT} If var is not declared or its value is null, $ DEFAULT is used as its value.
$ {Var + OTHER} If var is declared, its role is $ OTHER. Otherwise, it is a NULL string.
$ {Var: + OTHER} If var is set, its value is $ OTHER. Otherwise, it is a NULL string.
$ {Var? ERR_MSG} If var is not declared, print $ ERR_MSG
$ {Var :? ERR_MSG} If var is not set, print $ ERR_MSG
$ {! Varprefix *} matches all variables declared starting with varprefix.
$ {! Varprefix @} matches all variables declared starting with varprefix.
2. string operations (length, read and replace)
$ {# String} $ string length
$ {String: position} in $ string, the substring is extracted from the position $ position.
$ {String: position: length} in $ string, the substring with the length of $ length is extracted from the position $ position.
Starting with $ string, $ {string # substring} deletes the substring that matches the minimum $ substring value.
$ {String ## substring} starts with the variable $ string and deletes the substrings that match the longest $ substring.
$ {String % substring}: removes the substring that matches the minimum $ substring value from the end of $ string.
$ {String % substring}: removes the substring that matches $ substring at the end of $ string.
$ {String/substring/replacement} uses $ replacement to replace the first matched $ substring.
$ {String // substring/replacement} uses $ replacement instead of all matched $ substring
$ {String/# substring/replacement} if the $ string prefix matches $ substring, $ replacement is used to replace the matched $ substring.
$ {String/% substring/replacement} if the $ string suffix matches $ substring, $ replacement is used to replace the matched $ substring.
This article is from "Zhang Libin"