Linux Shell intercepts strings

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.