Shell Script Array Operation summary _linux shell

Source: Internet
Author: User

The Linux shell is much more powerful in programming than in Windows batching, whether in cycles or operations.

Bash supports one-dimensional arrays (multidimensional arrays are not supported) and does not qualify the size of the array. Similar to the C language, the subscript of an array element is numbered starting with 0. Gets the elements in the array to use the subscript, the subscript can be an integer or an arithmetic expression with a value greater than or equal to 0.

Defining arrays

In the shell, parentheses are used to represent the array, and the array elements are separated by a "space" symbol. The general form of the definition array is:

Copy Code code as follows:
Array name = (value 1 value 2 ...) Value N)

For example:
Copy Code code as follows:

Array_name= (value0 value1 value2 value3)

Or
Copy Code code as follows:

Array_name= (
Value0
Value1
value2
Value3
)

You can also define individual components of an array individually:

Copy Code code as follows:

Array_name[0]=value0
Array_name[1]=value1
Array_name[n]=valuen

You can not use consecutive subscript, and there is no limit to the scope of the subscript.

Reading arrays

The general format for reading array element values is:

Copy Code code as follows:

${array name [subscript]}

For example:

Copy Code code as follows:

Valuen=${array_name[n]}

Use the @ symbol to get all the elements in an array, for example:

Copy Code code as follows:

Echo ${array_name[@]}

Get the length of an array

The method of getting the length of an array is the same as getting the length of the string, for example:

Copy Code code as follows:

# Gets the number of array elements
length=${#array_name [@]}
# or
length=${#array_name [*]}
# Gets the length of an array of individual elements
lengthn=${#array_name [n]}

Appendix: Summary of Shell arrays

Do not know when to write things, the document was archaeological discovery, to those who idle egg pain, a smile. If the error in this article brings you all the spiritual loss, please find the insurance company! Of course you can tell me (talk)

arrays, as a special kind of data structure, have a place in any programming language, and of course bash shells are no exception. This article makes a small summary of the shell array.
Here we only discuss the case of one-dimensional arrays, about multidimensional arrays (in fact, you have to use a one-dimensional array of methods to simulate), not involved. This includes the copying of arrays, computations, deletions, replacements.

The declaration of an array:

Copy Code code as follows:
Array[key]=value # Array[0]=one,array[1]=two

Declare-a Array # Array is treated as an array name

array= (value1 value2 value3 ...)

array= ([1]=one [2]=two [3]=three ...)

Array= "One Two Three" # Echo ${array[0|@|*]}, treat the array variable as an array, but the array element has only the string itself

Array Access:

Copy Code code as follows:
${array[key]} # ${array[1]}

The deletion of an array

Copy Code code as follows:
unset Array[1] # Delete the first element in the array

unset Array # Delete entire array

To calculate the length of an array:

Copy Code code as follows:
${#array}

${#array [0]} #同上. ${#array [*]}, ${#array [@]}. Note the difference between the same #{array:0}

Extraction of arrays

To start from the tail:

Copy Code code as follows:
array= ([0]=one [1]=two [2]=three [3]=four]
${ARRAY[@]:1} # Two three four, remove all elements after the first element, then ${array[@]:0} represents all elements
${array[@]:0:2} # One Two
${array[@]:1:2} # Two Three

Sub-string deletion

Copy Code code as follows:

[Root@localhost dev]# Echo ${array[@]:0}
One two three four

[Root@localhost dev]# Echo ${array[@] #t *e} # Start the shortest match on the left: ' T*e ', which will match to ' thre '
One two E four

[Root@localhost dev]# echo ${array[@]# #t *e} # Start the longest match on the left, which will match to ' three '

[Root@localhost dev]# array= ([0]=one [1]=two [2]=three [3]=four]]

[Root@localhost dev]# Echo ${array[@]%o} # Start the shortest match from the end of the string
One TW three Four

[Root@localhost dev]# Echo ${array[@]%%o} # start the longest match from the end of the string
One TW three Four

Child string Substitution

Copy Code code as follows:
[Root@localhost dev]# array= ([0]=one [1]=two [2]=three [3]=four]]

The first one to be matched will be deleted.

Copy Code code as follows:
[Root@localhost dev]# Echo ${array[@]/o/m}
Mne TWM Three Fmur

All matches will be deleted.

Copy Code code as follows:
[Root@localhost dev]# Echo ${array[@]//o/m}
Mne TWM Three Fmur

No replacement substring is specified, then a matching substring is deleted

Copy Code code as follows:
[Root@localhost dev]# Echo ${array[@]//o/}
NE tw three fur

Replacement string Front terminal string

Copy Code code as follows:
[Root@localhost dev]# Echo ${array[@]/#o/k}
Kne two Three Four

Replacement string Rear terminal string

Copy Code code as follows:
[Root@localhost dev]# Echo ${array[@]/%o/k}
One TWK three Four

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.