A one-dimensional array in the shell is worth getting

Source: Internet
Author: User

(1) Definition of an array

[Email protected]:~# a= (1 2 3 4)

[Email protected]:~# echo ${a[1]}
2

[Email protected]:~# a[0]=1
[Email protected]:~# a[1]=2

[Email protected]:~# echo ${a[0]}
1

(2) Array worth acquiring

[Email protected]:~# var[0]=1
[Email protected]:~# var[1]=2

[Email protected]:~# echo $var [1]
1[1]
[Email protected]:~# echo ${var[1]}
2

(3) Gets the length of the array. use ${#数组名 [@ or *]} to get the array length

[Email protected]:~# a= (10 20 30 40 50 60)
[Email protected]:~# echo ${#a [*]}
6
[Email protected]:~# echo ${#a [@]}
6

[Email protected]:~# echo ${a[@]}
10 20 30 40 50 60

[Email protected]:~# echo ${a[*]}
10 20 30 40 50 60

(4) array read, with ${array name [subscript]} subscript is starting from 0 subscript is: * or @ Get the entire array contents
[Email protected]:~# echo ${a[1]}
20

[Email protected]:~# echo ${a[*]}
10 20 30 40 50 60

(5) Assigning values to arrays

[Email protected]:~# a[3]=100

[Email protected]:~# echo ${a[*]}
10 20 30 100 50 60

(6) The deletion of the array, directly through: unset array [subscript] can clear the corresponding element, without subscript, clear the entire data.

[Email protected]:~# echo ${a[*]}
10 20 30 100 50 60
[Email protected]:~# unset a[1]
[Email protected]:~# echo ${#a [@]}
5

[Email protected]:~# echo ${a[*]}

[Email protected]:~# unset a
[Email protected]:~# echo ${#a [*]}
0

(7) Shard use, directly through the ${array name [@ or *]: Start position: Length} Slice the original array, return is a string, the middle with "space" separate, so if you add "()", will get the slice array, the above example: C is a new data.

A:TT is just a variable, not an array, so be sure to add it ()

[Email protected]:~# Tt=${a[*]:2:4}

[Email protected]:~# Echo $TT 30 40 50 60

[Email protected]:~# echo ${tt[1]}

[Email protected]:~# echo ${tt[2]}

B:C is still an array

[Email protected]:~# echo ${a[*]}
10 20 30 40 50 60

[Email protected]:~# c= (${a[*]:1:4})

[Email protected]:~# echo ${c[*]}
20 30 40 50
[Email protected]:~# Echo $c
20

[Email protected]:~# echo ${c[2]}
40
[Email protected]:~# echo ${a[*]}
10 20 30 40 50 60

(8) Replace, The Call method is: ${array name [@ or *]/find character/substitution character} This operation will not change the original array contents, if you need to modify, you can see the above example, redefine the data.

[Email protected]:~# echo ${a[*]}
10 20 30 40 50 60
[Email protected]:~# Echo ${a[*]/60/100}
10 20 30 40 50 100

[Email protected]:~# echo ${a[*]} //The value of array A is constant
10 20 30 40 50 60

[Email protected]:~# tt=${a[*]/100/1000} //Without parentheses, it is a variable, not an array
[Email protected]:~# Echo $tt
10 20 30 40 50 60

[Email protected]:~# echo ${tt[1]}

[Email protected]:~# tt= (${a[*]/60/1000}) //with parentheses, then TT is an array

[Email protected]:~# echo ${tt[5]}

1000

[Email protected]:~# echo ${tt[*]}
10 20 30 40 50 1000
[Email protected]:~# echo ${a[*]} //Array A is unchanged
10 20 30 40 50 60

A one-dimensional array in the shell is worth getting

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.