Shell string Handling

Source: Internet
Author: User
Tags first string

String slices:

${var:offset:number}

Offset: The number of strings to skip

Number: The string to be fetched

Cases:

Name= "Obama"

${name:1:2}--Displays the result as Ba

${name:1}--Displays the result as Bama

Take the rightmost few characters of the string: ${var:-legh}:

Note: There must be a blank character after the colon;

#echo ${name:-3}

To take a substring based on a pattern:

Match from left to right

${var#*word}: Where word can be any character specified; function: From left to right, look for the first occurrence of word (character) in the string stored by the var variable, Delete all characters from the beginning of the string to the first occurrence of Word characters (including word characters);

${var##*word}: Ditto, however, deletes everything between the beginning of the string and the last character that Word refers to

Match from right to left

${var%word*}: Where word can be any character specified; function: From left to right, find the var variable stored in the string, the first occurrence of word (character), Delete the string open the last character to the left to the first occurrence of all characters between word characters;

${var%%word*:}: Ibid., however, deletes all the contents of the string from the end to the first word character

Example: usr=http://www.magedu.com:80

#${usr##*:}: Fetch is 80

#${usr%%:*}: HTTP is the most

Find Replacements:

${var/pattern/substi}:

To find the string represented by Var, the first string to be matched to by pattern, replaced by Substi;

#user =$ (head-1/etc/passwd)

#echo ${user/root/root}

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/96/4D/wKioL1kfEhaQh2TuAAAHwS2rgsg060.png "title=" Picture 1.png "alt=" Wkiol1kfehaqh2tuaaahws2rgsg060.png "/>

${var//pattern/substi}:

Find the string represented by Var, all the strings that can be matched to by pattern, and replace them with Substi;

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/96/4C/wKiom1kfEkjyDAQaAAAIIwOcvg0299.png "title=" Picture 2.png "alt=" Wkiom1kfekjydaqaaaaiiwocvg0299.png "/>

${var/#pattern/substi}:

Find the string represented by Var, the string that the beginning of the line is matched to by pattern, and replace it with Substi;

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/96/4D/wKioL1kfEnqy4DwjAAAQ1ArBINw316.png "title=" Picture 3.png "alt=" Wkiol1kfenqy4dwjaaaq1arbinw316.png "/>

${var/%pattern/substi}:

Find the string represented by Var, the string that the end of the line is matched to by pattern, and replace it with Substi;

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/96/4D/wKioL1kfEqLD2cAnAAAJM8hKylA545.png "title=" Picture 4.png "alt=" Wkiol1kfeqld2canaaajm8hkyla545.png "/>

Find and Delete:

${var/pattern}: Finds the string represented by Var, the first time it is matched to the delete

${var//pattern}: Finds the string represented by Var, all of which are matched to delete

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/96/4C/wKiom1kfEvbjzs8rAAAJwp1UqnA672.png "title=" Picture 5.png "alt=" Wkiom1kfevbjzs8raaajwp1uqna672.png "/>

${var/#pattern}: Find the string represented by Var, the beginning of the line is matched to delete

${var/%pattern}: Find the string represented by Var, the end of the line is matched to delete

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/96/4D/wKioL1kfEwXwQuElAAAKps1yNM8728.png "title=" Picture 6.png "alt=" Wkiol1kfewxwquelaaakps1ynm8728.png "/>

Character-Case Conversions:

${var^^}: Converts all lowercase letters in VAR to uppercase:

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/96/4D/wKioL1kfE07wend6AAAIvhoRGA0755.png "title=" Picture 7.png "alt=" Wkiol1kfe07wend6aaaivhorga0755.png "/>

${var,}: Converts all uppercase letters in VAR to lowercase:

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/96/4C/wKiom1kfE1ris8xMAAAHrx7AYEo726.png "title=" Picture 8.png "alt=" Wkiom1kfe1ris8xmaaahrx7ayeo726.png "/>

Variable assignment:

${var:-value}: Returns value if Var is empty or not set, otherwise returns VAR

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/96/4D/wKioL1kfE4XCpBekAAAJ1dUFH9g173.png "title=" Picture 9.png "alt=" Wkiol1kfe4xcpbekaaaj1dufh9g173.png "/>

${var:=value}: If Var is empty or not set, then value is assigned to Var and returned (equals set default value)

${var:+value}: Returns value if Var is different, just as opposed to ${var:-value}

${var:?error_info}: Returns Error_info if Var is empty or not set, otherwise returns the value of Var


This article is from the "burning Years of Passion" blog, please be sure to keep this source http://liuzhengwei521.blog.51cto.com/4855442/1927718

Shell string Handling

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.