Shell Script Programming Overview (IV): Bash built-in character processing tool

Source: Internet
Author: User

First, string slices:

${var:offset:number} takes a substring of a string;

Take the rightmost few characters of the character toe: ${var:-length}

Note: There must be a white space character after the colon;

Second, To take a substring based on a pattern:

1.${var#*word}

Word is the specified delimiter;

From left to right, look for the first occurrence of the word delimiter in the string stored by the var variable, and delete all characters from the beginning of the string to the delimiter

2.${var##*word}

From left to right, look for the last occurrence of the word delimiter in the string stored by the var variable, removing the string from the beginning of all the characters between the delimiters

3.${var%word*}

From right to left, look for the first occurrence of the word delimiter in the string stored by the var variable, removing the delimiter to all characters between the trailing end of the string

4.${var%%word*}

From right to left, look for the last word delimiter that appears in the string stored by the var variable, removing the delimiter to all characters between the trailing end of the string

Example:

Mypath= "/etc/init.d/functions"

${mypath##*/}: Functions

${mypath#*/}: Etc/init.d/functions

${mypath%/*}:/ETC/INIT.D

Url=http://www.magedu.com:80

${url##*:}

${url%%:*}

Third, Find Replacements

Note: Use Glob style and wildcard characters in pattern;

(1) ${var/pattern/substi}

Finds the string in the string represented by Var that was first matched to by pattern, replacing it with the string represented by Substi

(2) ${var//pattern/substi}

Finds all strings that are matched by pattern in the string represented by Var and replaces them all with the string represented by Substi

(3) ${var/#PATTERN/substi}

Finds the string represented by Var, and replaces it with the string represented by Substi by the string to which the beginning of the line is matched by the pattern;

(4) ${var/%pattern/substi}

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

Four, Find Delete:

${var/pattern}: Finds the first match in the Var string in pattern mode and deletes it;

${var//patern}

${var/#PATTERN}

${var/%pattern}

Five, character-case conversions:

${var^^}: Converts all lowercase characters in var to uppercase;

${var,}: Converts all uppercase characters in var to lowercase;

Six, variable Assignment:

${var:-value}: Returns value if the var variable is empty or not set, otherwise returns the var variable;

${var:=value}: If the var variable is empty or not set, then return value and assign value to the var variable, otherwise return the value of the Var variable;

${var:+value}: Returns VALUE if the var variable is not empty;

${var:? Error_info}: If Var is empty, or not set, then return error_info as error; otherwise, return var value;


This article is from the "Xu Ding blog" blog, make sure to keep this source http://xuding.blog.51cto.com/4890434/1740550

Shell Script Programming Overview (IV): Bash built-in character processing tool

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.