Array and string processing tools for Linux-shell programming

Source: Internet
Author: User
Tags first string

Array: A contiguous memory space that holds multiple elements.

Declaring an array: bash-4 support In addition to the default 0,1,2 ... You can also customize the index format, which is called an associative array

Declaring an indexed array: declare-a NAME

Declaring associative arrays: declare-a NAME

Indexed array Assignment:

Indexed mode assignment: Array_name[index]=value

Array_name= ("value1" "value2" ...)

Array_name=value ([0]= "value1" [3]= "value2")

Interactive assignment: Read-a array_name

Associative array Assignment

Array_name= ([index_name1]= "VALUE1") [index_name2]= "VALUE2") ...)

Array index: The Position property that represents the array where the element is located, starting with 0.

Format: array name [index]

Referencing an element in an array: ${array_name[index]}

When index is not given the default value is 0, you must use ${} when referencing an element value in an array.

Array slices:

${array_name[@]:offset:number}

Offset: The number of elements to skip

Number: The count of elements to be removed, and omitting numbers, representing all elements after the offset

add element:

array_name[${#ARRAY_NAME [*]}]=

${array_name[*]}: Number of elements in an array

To delete an element:

Unset Array[index]

String processing:

String slices:

${var:offset:number}, takes a substring of the string.

${var:-length}: Takes a length character from the right.

Note: Be sure to have a space after you take number 8th.

To take a substring based on a delimiter

${var#*word}:

WORD: The specified delimiter.

Function: From left to right, look for the first occurrence of the word delimiter in the string stored in the var variable, removing all characters from the beginning of the string to the delimiter.

${var##*word}:

WORD: The specified delimiter.

Function: From left to right, look for the last occurrence of the word delimiter in the string stored in the var variable, removing all characters from the beginning of the string to the delimiter.

      

${var%word*}:

WORD: The specified delimiter.

Function: From right to left, look for the first occurrence of the word delimiter in the string stored in the var variable, removing all characters from the end of the string to this delimiter.

${var%%word*}:

WORD: The specified delimiter.

Function: From right to left, look for the last occurrence of the word delimiter in the string stored in the var variable, removing all characters from the end of the string to this delimiter.

      

Find Replacements:

${var/pattern/substitute}: Finds the string represented by Var, replacing the first string that is matched by PATTERN with the string represented by substitute.

${var//pattern/substitute}: Finds the string represented by Var, replacing all strings that are matched by PATTERN with the string represented by substitute.

${var/#PATTERN/substitute}: Finds the string that is represented by VAR, and replaces it with the string represented by substitute by the string that the beginning of the line is matched by PATTERN.

${var/%pattern/substitute}: Finds the string represented by Var, and replaces it with the string represented by the substitute, in the string that the end of the line is matched by PATTERN.

Find Delete:

${var/patter}: Deletes the first string in Var that is matched by pattern.

${var//patter}: Deletes all strings in Var that are matched by pattern.

${var/#PATTER}: Deletes the first string of the Var row by the pattern match.

${var/%patter}: Deletes the end of the Var line by the pattern-matching string.

Case conversion:

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

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

Variable assignment:

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

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

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

${var:+value}: If the var variable is not empty, then return VALUE;

    

  

  

  

Array and string processing tools for Linux-shell programming

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.