In Linux systems, Bash supports a particularly large number of syntax/tools for string operations, but there is no unified standard for these operations. Some string operations are subsets of parameter replacement, and others use the expr command.
This time, we are explaining the operation of parameter replacement strings. Interested people can understand the usage of expr string replacement on their own. Personally, I believe that the method is only used to solve the problem and master the usage of simple and fast string replacement.
1.${string:position:length}
Extract the substring of length length from position position in string.
2. Example
Example of operation string: string=abc123ABC456xyz
Index subscript starts at 0 0123456789............
String operations start from the left by default
2.1. Extract all string strings
Order:
echo ${string:0}
[root@rhel77 ~]# string=abc123ABC456xyz [root@rhel77 ~]# echo ${string:0} abc123ABC456xyz [root@rhel77 ~]#
2.2. Starting from the 7th position, extract the remaining substring of string
Order:
echo ${string:7}
[root@rhel77 ~]# echo ${string:7} BC456xyz [root@rhel77 ~]#
2.3. Starting from bit 7, extract the string substring with length 3.
echo ${string:7:3}
[root@rhel77 ~]# echo ${string:7:3} BC4 [root@rhel77 ~]#
2.4. Extract substrings with length 4 from the right side of string
Using parentheses() or using a space to "escape" positional parameters, you can implement string to extract substrings from the right
Order:
echo ${string:(-4)}
OR
echo ${string: -4}
[root@rhel77 ~]# echo ${string:(-4)} 6xyz [root@rhel77 ~]# [root@rhel77 ~]# echo ${string: -4} 6xyz [root@rhel77 ~]#
This is the article about the specific use of bash extracting string ${string:position:length}. For more related bash extracting string content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!