Linux Shell intercepts strings
There are many ways to intercept strings in the shell
${var#*/}${var##*/}${var%/*}${var%%/*}${var:start:len}${var:start}${var:0-start : Len}${var:0-start}
Here are a few examples to illustrate:
1) Get the length of the string
Grammar:
${#var}
Example code:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"Echo " string: [${str}] " length=${#str}echo"length: [${length}]"
Execution Result:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]length: [+]
2) use # and # # to get the trailing substring 2.1) # min. intercept word from the front
Grammar:
${parameter#word}
Example code:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"Echo " string: [${str}] " #分割符为 ' / ' substr=${str#*/}echo"substr: [${substr}]"
Execution Result:
string : [http:///www.fengbohello.xin3e.com/blog/shell-truncating-string]substr: [/ www.fengbohello.xin3e.com/blog/shell-truncating-string]
2.2) # # Max intercept word from front
Grammar:
${parameter# #word}
Example code:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"Echo " string: [${str}] " #分割符为 ' / ' substr=${str##*/}echo"substr: [${substr}]"
Execution Result:
string : [http:///www.fengbohello.xin3e.com/blog/shell-truncating-string]substr: [ shell-truncating-string]
3) Use% and percent to get the head substring 3.1)% min. intercept word from behind
Grammar:
Example code:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"Echo " string: [${str}] " substr=${str%/*}echo "substr: [${substr}]"
Execution Result:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]substr: [http://Www.fengbohello.xin3e.com/blog]
3.2) percent of the maximum to intercept word from behind
Grammar:
${parameter%%word}
Example code:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"Echo " string: [${str}] " substr=${str%%/*}echo "substr: [${substr}]"
Execution Result:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]substr: [http:]
4) Use ${var:} mode to get SUBSTRING 4.1) Specify the number of characters from the left and the substring of the string
Grammar:
${var:start:len}
Example code:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"Echo " string: [${str}] " 0 means the first character on the left begins, and7 indicates the total number of child characters. substr=${str:0:7}echo"substr: [${substr}] "
Execution Result:
string : [http:///www.fengbohello.xin3e.com/blog/shell-truncating-string]substr: [http: // ]
4.2) Start from the first few characters on the left to the end
Grammar:
${var:7}
Example code:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"Echo " string: [${str}] " 7 means the 8th character on the left begins substr=${str:7}echo" SUBSTR: [${substr}]"
Execution Result:
string : [http:///www.fengbohello.xin3e.com/blog/shell-truncating-string]substr: [ www.fengbohello.xin3e.com/blog/shell-truncating-string]
4.3) Starting with the first few characters on the right and the number of characters
Grammar:
${var:0-start:len}
Example code:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"Echo " string: [${str}] " 0- up to the right, the 23rd character starts, and5 indicates the number of characters substr=${str:0-23 :5}echo"substr: [${substr}]"
Execution Result:
string : [http:///www.fengbohello.xin3e.com/blog/shell-truncating-string]substr: [Shell ]
4.4) Start from the first few characters on the right to the end
Grammar:
${var:0-start}
Example code:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"Echo " string: [${str}] " 0-6 means the 6th character starts at the right substr=${str:0-6} echo"substr: [${substr}]"
Execution Result:
string : [http:///www.fengbohello.xin3e.com/blog/shell-truncating-string]substr: [ String]
This article was posted synchronously here:http://www.fengbohello.xin3e.com/point/p/629
Storm
Mail: [Email protected]
Linux Shell intercepts strings