Common operations of variable quantum strings in shell scripts

Source: Internet
Author: User

String manipulation

${#string} The number of characters in the output sentence

${string:position:length} output $position The length of the start of the character

${string:position}

[[email protected] scripts]# liuyifei=] i like sanpan i like luo  Swimming "[[email protected] scripts]# echo ${#liuyifei}33[[email protected]  Scripts]# echo ${liuyifei:2:4}like[[email protected] scripts]# echo ${liuyifei:2 }  like sanpan i like luo swimming==== above measured      ${ String#substring} is retrieved from the front of the string, as long as the match to the character begins to delete the shortest match      ${string# #substring} from the beginning to the longest delete      ${string%substring} starts at the end of      ${string%%substring} from the end, Similar to above ##====== must be matched from the beginning on [[Email protected] scripts]# echo ${liuyifei#like}i like  sanpan i like luo swimming[[email protected] scripts]# echo ${ Liuyifei#*like}sanpan i like luo swimming[[email protected] scripts]# echo  ${liuyifei##*like}luo swimming[[email protected] scripts]# echo ${liuyifei%*like} i like  sanpan i like luo swimming[[email protected] scripts]# echo ${ Liuyifei%like} i like sanpan i like luo swimming[[email protected]  scripts]# echo ${liuyifei%like*}i like sanpan i[[email protected]  Scripts]# echo ${liuyifei%%like*}i

========================

${string/substring/replace} replaces the first encountered match character

${string/#substring/replace} prefix match, must match all, replace a strip

${string/%substring/replace} forward, replace the last encountered match character, must match all

[[email protected] scripts]# echo ${liuyifei/like/love}

I love Sanpan i like Luo swimming

[[email protected] scripts]# echo ${liuyifei/#*like/love}

Love Luo swimming

[[email protected] scripts]# echo ${liuyifei/%like*/love}

I love


Personal Summary: #% appears in the shell variable operation must always be matched for related operations

Common operations of variable quantum strings in shell scripts

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.