Basically, the articles referenced below add a few items to make a record, so you are too lazy to remember them, saving you the trouble of Google every time.
Http://my.oschina.net/aiguozhe/blog/41557
1. Get the length
It seems that the second type is generally used.
2. Locate the substring
3. Select substrings
2 |
Expr substr "$ Str" 1 3 # Take 3 characters from the first position, ABC |
3 |
Expr substr "$ Str" 2 5 # Start from the second position and take 5 characters, bcdef |
4 |
Expr substr "$ Str" 4 5 # Start from the fourth position with 5 characters, def |
6 |
Echo $ {STR: 2} # Extract the string from the second position, bcdef |
7 |
Echo $ {STR: 2: 3} # Extract 3 characters from the second position, BCD |
8 |
Echo $ {STR :(-2 )} # Extract the string from the second position to the left, ABCDE |
9 |
Echo $ {STR :(-2): 3} # Extract 6 characters from the penultimate position to the left, CDE |
4. Capture substrings
01 |
STR = "abbc, def, Ghi, abcjkl" |
02 |
Echo $ {STR # A * c} #, def, Ghi, abcjkl A well number (#) indicates the shortest matching from the left |
03 |
Echo $ {STR # A * c} # jkl, The two pound signs (#) represent the longest match on the left. |
04 |
Echo $ {STR # "A * C "}# Null, because STR does not contain the substring "A * C" |
05 |
Echo $ [STR ## "A * C "}# Null. Likewise |
06 |
Echo $ {STR # D * f) # abbc, def, Ghi, abcjkl, |
07 |
Echo $ {STR # * D * f} #, Ghi, abcjkl |
09 |
Echo $ {STR % A * l} # A percent sign (%) for abbc, def, and Ghi indicates the shortest match is intercepted from the right side. |
10 |
Echo $ {STR % B * l} # A two percentage signs (%) indicates the longest matching from the right side |
11 |
Echo $ {STR % A * c} # Abbc, def, Ghi, abcjkl |
It can be remembered that the pound sign (#) is usually used to represent a number, which is placed in front; The percent sign (%) is unmounted to the back of the number;
Or in this way, in the keyboard layout, the well number (#) is always on the left of the percent sign (%) (that is, the Front ):-)
5. String replacement
1 |
STR = "Apple, tree, apple tree" |
2 |
Echo $ {str/Apple} # Replace the first Apple |
3 |
Echo $ {STR // Apple/Apple} # Replace all apple |
5 |
Echo $ {str/# Apple/Apple }# If the STR string starts with Apple, replace it with Apple. |
6 |
Echo $ {str/% Apple/Apple} # If the string STR ends with Apple, replace it with apple |
6. Comparison
* A single group [] is acceptable.
1 |
[["A.txt" = A *] # Pattern matching) |
2 |
[["A.txt" = ~ . * \. Txt] # RegEx matching) |
3 |
[["ABC" = "ABC"] # Logical truth (string comparision) |
4 |
[["11" < "2"] # Logical truth (string comparision), compared by ASCII Value |
7. Connection
3 |
Echo ${S1 }$ {S2 }# Of course, you can write $ S1 $ S2 in this way, but you 'd better add braces. |
8. Save the command execution result in the variable.
* Note that the two sides are not single quotes.
9. Split the string into an array.
1 |
Arguments = ($ (echo $ {x} | tr "," "\ N "))# X is the input parameter, separated by commas |
10. case-insensitive Conversion
Debpackname ='Echo $2 | awk '{print tolower ($0 )}''
# Convert to lowercase
Debpackname ='Echo $2 | awk '{print toupper ($0 )}'`
# Convert to uppercase