Each language has its own string operation method, and the shell is the same. The following describes common methods in an example.
1. Get the string length
String = abc12342341 // do not have spaces on the second side of the equal sign echo $ {# string} // result 11 expr length $ string // result 11 expr "$ string ":". * "// result 11 there must be a space on the two sides of the semicolon. Here, the root match is similar in usage.
2. string location
Expr index $ string '200' // Result 4 the subscript corresponding to the string starts from 0.
This method reminds me of the JS indexof. The operations on strings in various languages are similar in the general direction. If there is a basic language, learning shell will be very fast.
3. the maximum length from the beginning of the string to the substring
Expr match $ string 'abc. * 3' // result 9
I personally think this function is of little use. Why should it start from the beginning.
4. String Truncation
Echo $ {string: 4} // 2342341 intercepts all the strings following the 4th-Bit String echo $ {string: 3: 3} // 123 capture the next three Echo $ {string: 3: 6} // 3rd capture the Next Six Echo $ {string: -4} // 2341: there is a space on the right side to intercept the last 4 echo $ {string :(-4 )} // 2341 same as expr substr $ string 3 3 // 123 the last three digits are intercepted starting from the first digit
The above method reminds me of the fact that the substr function of PHP is the same as the rules intercepted later.
5. Match the displayed content
// In Example 3, match is also different from match. The length of the matching character is displayed, the following is the matched content expr match $ string '\ ([a-c] * [0-9] * \)' // abc12342341 expr $ string: '\ ([a-c] * [0-9] \)' // ABC1 expr $ string :'. * \ ([0-9] [0-9] [0-9] \) '// 341 display matching content in parentheses
Is there any similarity between the brackets in the root and other brackets,
6. Capture unmatched content
Echo $ {string # A * 3} // 42341 starts from the left of $ string, remove the shortest matching substring echo $ {string # C * 3} // abc12342341 and nothing matches echo $ {string # * C1 * 3} // 42341 from the left side of $ string start, remove the shortest matching substring echo $ {string # A * 3} // 41 from the left of $ string, remove the longest matched substring echo $ {string % 3*1} // abc12342 from the right of $ string, remove the shortest matched substring echo $ {string % 3*1} // abc12 from the right of $ string, remove the longest matched substring
Note that it must start with the first character of the string or the last one,
7. Match and replace
Echo $ {string/23/BB} // abc1bb42341 replace echo $ {string // 23/BB} // abc1bb4bb41 double slash replace all matched echo $ {string/# ABC/ bb} // bb12342341 # start with what to match, ^ in the root php is a bit like Echo $ {string/% 41/BB} // What is the end of ECHO $ {string/% 41/BB} // abc123423bb %? $ in the root PHP is a bit like
From: http://blog.51yip.com/shell/1080.html
[Convert] shell string operation method and instance