Linux Shell Series Tutorial (vi) Shell array

Source: Internet
Author: User

This article is part (vi) of the Linux Shell Series tutorial, more on the Shell Tutorial: Linux Shell Series Tutorials

The shell is very powerful in programming, its array function is also very perfect, today we introduce the use of the shell array.

The shell supports one-dimensional arrays (which do not support multidimensional arrays) 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 take advantage of subscript, the subscript can be an integer or an arithmetic expression whose value should be greater than or equal to 0.

I. Definition of shell arrays

In the shell, the array is represented by parentheses, and the elements of the array are separated by a "space".

The general form of the definition array is:

Array_name= (value1 ... valuen)

For example:

Array_name= (value0 value1 value2 value3) array_name= (VALUE0VALUE1VALUE2VALUE3)

You can also define the individual components of an array individually:

Array_name[0]=value0array_name[1]=value1array_name[2]=value2

You can not use successive subscripts, and there is no limit to the range of subscripts.

Assignment of the shell array

It can be referenced directly by the array name [subscript], and if the subscript does not exist, a new array element is added automatically

Examples of Use:

[[email protected] ~]$ A[1]=100[[email protected] ~]$ echo ${a[*]} 1 3 4 5[[email protected] ~]$ a[5]=100     [email Protected] ~]$ echo ${a[*]}1 100 3 4 5 100
Iii. reading of the shell array

The general format for reading shell array element values is:

${ARRAY_NAME[INDEX]}VALUEN=${ARRAY_NAME[2]}

Examples of Use:

#!/bin/sh#www.linuxdaxue.comname[0]= "Zara" name[1]= "Qadir" name[2]= "Mahnaz" name[3]= "Ayan" NAME[4]= "Daisy" echo " First index: ${name[0]} "echo" Second Index: ${name[1]} "

Run script, Output:

$./test.shfirst Index:zarasecond Index:qadir

Use @ or * to get all the elements in the array, for example:

${array_name[*]}${array_name[@]}

Examples of Use:

#!/bin/sh#www.linuxdaxue.comname[0]= "Zara" name[1]= "Qadir" name[2]= "Mahnaz" name[3]= "Ayan" NAME[4]= "Daisy" echo " First method: ${name[*]} "echo" Second Method: ${name[@]} "

Run script, Output:

$./test.shfirst Method:zara Qadir Mahnaz Ayan daisysecond Method:zara Qadir Mahnaz Ayan Daisy
Iv. removal of shell arrays

Directly through: unset array [subscript] can clear the corresponding element, without subscript, clear the entire data.

Using the example

[[email protected] ~]$ a= (1 2 3 4 5) [[email protected] ~]$ unset a[[email protected] ~]$ echo ${a[*]}[[email protected] ~] $ a= (1 2 3 4 5) [[email protected] ~]$ unset a[1]   [[email protected] ~]$ echo ${a[*]} 1 3 4 5[[email protected] ~]$ Ech o ${#a [*]}4
V. Other common operations for Shell Arrays 1) Shell array length

The array length can be obtained with ${#数组名 [@ or *]}, as shown in the following example:

# Gets the number of array elements length=${#array_name [@]}# or length=${#array_name [*]}# Gets the length of the single element of the array lengthn=${#array_name [n]}
2) Shard of shell Array

Directly through the ${array name [@ or *]: Start position: Length} Slice the original array, return is a string, the middle is separated by "space", so if you add "()", you will get the slice array.

Examples of Use

[[email protected] ~]$ a= (1 2 3 4 5) [[email protected] ~]$ echo ${a[@]:0:3}1 2 3[[email protected] ~]$ echo ${a[@]:1:4}2 3 4 5[[email protected] ~]$ c= (${a[@]:1:4}) [[email protected] ~]$ echo ${#c [@]}4[[email protected] ~]$ echo ${c[*]} 2 3 4 5
3) Substitution of shell arrays

An array is replaced by the ${array name [@ or *]/find character/replace character} The operation does not change the original array contents, and if necessary, you can see the example above to redefine the data.

Examples of Use

[[email protected] ~]$ a= (1 2 3 4 5)    [[email protected] ~]$ echo ${a[@]/3/100}1 2 + 4 5[[email protected] ~]$ echo $ {a[@]}1 2 3 4 5[[email protected] ~]$ a= (${a[@]/3/100}) [[email protected] ~]$ echo ${a[@]}     1 2 100 4 5

Well, about the shell array of the contents of the fiber are introduced here, I hope we have a lot of practice, master.

For more shell tutorials See: Linux Shell Series Tutorials

This article fixed link: Linux University network _linux learning _shell_ embedded Linux--linux Shell Series tutorial (vi) Shell array

Linux Shell Series Tutorial (vi) Shell array

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.