Shell String Segmentation

Source: Internet
Author: User
Tags first string truncated

Syntax 1:substring=${string:start:len}

The subscript of string starts with 0, with start, but the Len character is truncated and assigned to substring

1 #!/bin/bash2 #substr =${string: Start:len}3 str=" 123456789 " 4 substr=${str:3:3}5echo $substr6 7 Output: 8     456

Syntax 2: intercepting a specified string

${varible##*string}: The string after the last string is truncated from left to right
${varible#*string}: The string after the first string is truncated from left to right
${varible%%string*}: The string after the last string is intercepted from right to left
${varible%string*}: The string after the first string is intercepted from right to left
"*": just a wildcard character can not

1#!/bin/Bash2 3Str="Usr/bin/user"4#* ${varible##*string}5substr1=${str##*/}6echo $substr 1 # =User7 8#* ${varible#*string}9substr2=${str#*/}Tenecho $substr 2 # = bin/User One  A#* ${varible%%string*} -substr3=${str%%/*} - echo $substr 3 # = usr the  - #* ${varible%string*} - substr4=${str%/*} - echo $substr 4 # = Usr/bin

Shell String Segmentation

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.