String interception of shell scripts under Linux

Source: Internet
Author: User


1 #!/bin/bash

2

3 str= "Http://www.baidu.com/test.htm"

4

5 #echo ${str}

The result of the operation is:http://www.baidu.com/test.htm

Method One: #截取, delete the left character, leave the right character

Echo ${str#*//}

#*//means to delete the first//number and all characters on the left from the left, that is, delete http://

The result of the operation is:www.baidu.com/test.htm

Method Two: # #截取, delete the left character, leave the right character

Echo ${str##*/}

##*/means to delete the last/(i.e. rightmost/) and all characters on the left

The result of the operation is: test.htm

Method Three:% intercept, delete right character, preserve left character

Echo ${str%/*}

%/* means to remove all characters from the first/and right, starting at the far right

The result of the operation is:http://www.baidu.com

Method Four: Percent intercept, delete the right character, leave the left character

Echo ${str%%/*}

%%/* means delete the last/(leftmost/start) and all characters to the right starting from the right

Run Result:http:

Method Five: Start with the first few characters on the left and the number of characters

Echo ${str:0:5}

0 means starting with the first character on the left and 5 representing the total number of characters

The result of the operation is:http:

Method Six: Start from the first character of the coordinates until the end of the string

Echo ${str:7}

7 means starting from the eighth character of a string until the end of the string

The result of the operation is:www.baidu.com/test.htm

Method Seven: Start with the first few characters on the right and the number of characters

Echo ${str:0-7:3}

0-7 means starting with the first character on the right and 3 representing the number of characters

Run Result:est

Method Eight: Start from the first character on the right to the end

Echo ${str:0-7}

Starts at the seventh character on the right, until the end

The result of the operation is:est.htm



String interception of shell scripts under Linux

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.