Simple string operations in Shell

Source: Internet
Author: User

In shell programming, some string variables are often processed. For example, commands such as awk, expr, sed, and TR are often used to calculate length, intercept substring, and replace character. Next we will introduce a simple string processing method, without the need to nest complex sub-commands.

  1. $ {# Value}: Calculates the number of characters in the value string.
  2. $ {Value%.*}Or$ {Value%.*}: Delete the Left character that matches the separator "." In the value string and keep the right character.
  3. $ {Value#*.}Or$ {Value##*.}: Delete the right character that matches the separator "." In the value string and keep the Left character.
  4. $ {Value/Old/new}Or$ {Value//Old/new}: Replace the old substring that matches the value string with the new substring.

Supplement:"*" Indicates a wildcard used to match the string to be deleted. "." Indicates the Separator in the string, which can be any one or more characters. "%" Indicates a match from the right to the left, "#" indicates a match from the left to the right, and "\" indicates a replacement, all of which are non-greedy matches, that is, the minimum result matching the wildcard. Similar to "%", "#", and "/", "%", "#", and "//" are greedy matches, that is, the maximum result of matching the wildcard.

  1. $ {Value: Offset}Or$ {Value: Offset: length}: Truncate the substring from the left of the value string.
  2. $ {Value: 0-offset}Or$ {Value: 0-offset: length}: Truncate the substring from the right of the value string.

Supplement:The first character on the left starts from "0" and the first character on the right starts from "0-1. Offset starts with offset characters, and length indicates the length of the characters to be truncated. If there is no length variable, it indicates that the offset starts from the end of the string.

  1. $ {Value:-Word}: If the variable is undefined or the value is null, the return value is the content of the word; otherwise, the value of the variable is returned.
  2. $ {Value: = Word}: If the variable is undefined or the value is null, return the value of word and assign the value of Word to the value; otherwise, return the value of the variable.
  3. $ {Value: + Word}: When a variable has been assigned a value, the value is replaced by word. Otherwise, no replacement is performed.
  4. $ {Value:? Message}: When the variable has been assigned a value, it is replaced normally. Otherwise, the message is sent to the standard error output (if this replacement occurs in the shell program, the program stops running ).

Supplement:Word can be a string or a variable. When it is a variable, you need to use "$" to reference the variable.

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.