Linux Shell series (6) Shell array and shell Array

Source: Internet
Author: User

Linux Shell series (6) Shell array and shell Array

This article is part 6 of the Linux Shell series tutorials. For more shell tutorials, see the Linux Shell series tutorials.

Shell is very powerful in programming, and its array function is also very perfect. Today we will introduce the usage of Shell array.

Shell supports one-dimensional arrays (multidimensional arrays are not supported), and the array size is not limited.

Similar to the C language, the subscript of an array element starts from 0. To obtain the elements in an array, use the subscript. The subscript can be an integer or an arithmetic expression, and its value must be greater than or equal to 0.

I. Definition of Shell Array

In Shell, brackets are used to represent arrays. array elements are separated by spaces.

The general format of the defined array is:

Array_name = (value1... Valuen)

For example:

array_name=(value0 value1 value2 value3)array_name=(value0value1value2value3)

You can also separately define the components of the array:

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

Continuous subscripts are not allowed, and the range of the subscripts is unlimited.

2. assign values to Shell Arrays

The array name [subscript] can be used to reference and assign values. If the subscript does not exist, a new array element is automatically added.

Example:

[linuxdaxue@centos5 ~]$ a[1]=100[linuxdaxue@centos5 ~]$ echo ${a[*]} 1 100 3 4 5[linuxdaxue@centos5 ~]$ a[5]=100     [linuxdaxue@centos5 ~]$ echo ${a[*]}1 100 3 4 5 100
3. Read Shell Array

The general format for reading Shell array element values is:

${array_name[index]}valuen=${array_name[2]}

Example:

#!/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 the script and output:

$./test.shFirst Index: ZaraSecond Index: Qadir

You can use @ or * to obtain all elements in the array, for example:

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

Example:

#!/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 the script and output:

$./test.shFirst Method: Zara Qadir Mahnaz Ayan DaisySecond Method: Zara Qadir Mahnaz Ayan Daisy
4. Delete Shell Arrays

You can use the unset array [subscript] to clear the corresponding elements without any subscript.

Example

[linuxdaxue@centos5 ~]$ a=(1 2 3 4 5)[linuxdaxue@centos5 ~]$ unset a[linuxdaxue@centos5 ~]$ echo ${a[*]}[linuxdaxue@centos5 ~]$ a=(1 2 3 4 5)[linuxdaxue@centos5 ~]$ unset a[1]   [linuxdaxue@centos5 ~]$ echo ${a[*]} 1 3 4 5[linuxdaxue@centos5 ~]$ echo ${#a[*]}4
5. Other common Shell Array Operations 1) Shell array Length

You can use $ {# array name [@ or *]} to obtain the array length, for example:

# Obtaining the number of array elements length =$ {# array_name [@]} # Or length =$ {# array_name [*]} # obtaining the length of a single array element lengthn =$ {# array_name [n]}
2) shards of the Shell Array

Use $ {array name [@ or *]: Start position: length} to segment the original array. The returned value is a string separated by a space. Therefore, if you add "()", the Slice array is obtained.

Example

[linuxdaxue@centos5 ~]$ a=(1 2 3 4 5)[linuxdaxue@centos5 ~]$ echo ${a[@]:0:3}1 2 3[linuxdaxue@centos5 ~]$ echo ${a[@]:1:4}2 3 4 5[linuxdaxue@centos5 ~]$ c=(${a[@]:1:4})[linuxdaxue@centos5 ~]$ echo ${#c[@]}4[linuxdaxue@centos5 ~]$ echo ${c[*]} 2 3 4 5
3) Shell array replacement

The array replacement method is $ {array name [@ or *]/find character/replace character}. This operation does not change the original array content. If you need to modify it, you can refer to the above example, redefine data.

Example

[linuxdaxue@centos5 ~]$ a=(1 2 3 4 5)    [linuxdaxue@centos5 ~]$ echo ${a[@]/3/100}1 2 100 4 5[linuxdaxue@centos5 ~]$ echo ${a[@]}1 2 3 4 5[linuxdaxue@centos5 ~]$ a=(${a[@]/3/100}) [linuxdaxue@centos5 ~]$ echo ${a[@]}     1 2 100 4 5

Let's take a look at the Shell array content. I hope you can practice more and be proficient in it.

For more shell tutorials, see the Linux Shell series tutorials.

Fixed Link to this article: linux 网_linux lear_shell _ embedded Linux -- Linux Shell series tutorial (6) 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.