Linux: Bash Variable Processing

Source: Internet
Author: User

Variable can be extended to produce various functions.Generating a new result string does not affect the original variable!

 

Search, replace, and delete

A =/12/123/ABC

Replace the first appearance place from left to right: $ {A/12/}=>/// 123/ABC

Place where the first appearance starts from left to right: $ {A/#12/}=>/12/123/ABC, and $ {A/# \/12/} =>/123/ABC

Place where the first appearance starts from right to left: $ {A/% ABC/} =>/12/123 /, $ {A/% \/ABC/} =>/12/123

Replace all occurrences: $ {A // 12/} => // 3/ABC

 

 

Delete pattern matching

A =/12/123/ABC

Delete the left mode and the shortest match: $ {A/# * 12} =>/123/ABC

Delete the left mode and longest match: $ {A/# * 12} => 3/ABC

Delete the right mode and the shortest match: $ {A/% 12 */} =>/12/

Delete right mode and longest match: $ {A/% 12 */} =>/

 

Variable substring

A =/12/123/ABC

Extract the string from the start position on the left: $ {Variable: Start position (non-negative) }:: {A: 2 }=>/123/ABC

Offset from the right to the start position, and capture the string on the right [or the string from the start position on the right to the left]: $ {Variable: Start position (negative number )}: $ {A :(-2) }=> BC, note that negative numbers must be included

Extract the string of the specified length from the start position on the left to the right: $ {Variable: Start position (non-negative): length (non-negative) }:$ {A: 2: 7 }=> 2/123/

Offset from the left to the start position from the Right, and extract the string of the specified length from the right: $ {Variable: Start position (non-negative): length (non-negative )}: $ {A :(-2): 1 }=> B. Note: If the length is greater than or equal to the absolute value of the start position, it is equivalent to the truncation function.

 

Calculate the length of the variable's substring

A =/12/123/ABC

$ {# A} = 11

 

 

 

 

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.