Arrays in the shell

Source: Internet
Author: User

Defining arrays
A= (1 2 3 4 5 6)
Print array
echo ${a[@]} or echo ${a[*]}

[[email protected] shell]# a=(1 2 3 4 5 6)[[email protected] shell]# echo ${a[@]}1 2 3 4 5 6[[email protected] shell]# echo ${a[*]}1 2 3 4 5 6

Print the elements in an array by subscript

Gets the number of array elements
echo ${#a [@]}

[[email protected] shell]# echo ${#a[@]}6

The assignment of an array exists, then the substitution does not exist, and the "position by subscript" is added.

[[email protected] shell]# a[6]=100[[email protected] shell]# echo ${a[@]}1 2 3 4 5 6 100[[email protected] shell]# a[6]=aa[[email protected] shell]# echo ${a[@]}1 2 3 4 5 6 aa

Array Delete element "position by subscript"

Slices of an array

[[email protected] shell]# b=(`seq 1 10`)[[email protected] shell]# echo ${b[@]}1 2 3 4 5 6 7 8 9 10[[email protected] shell]# echo ${b[@]:3:4}  #从下标为三的元素开始截取 截取4个4 5 6 7[[email protected] shell]# echo ${b[@]:0-3:2}  #从下标为倒数第三的元素开始截取 截取2个8 9

Substitution of array elements

[[email protected] shell]# echo ${b[@]}1 2 3 4 5 6 7 8 9 10[[email protected] shell]# echo ${b[@]/3/33}    #只在显示结果里替换元素 数组不变1 2 33 4 5 6 7 8 9 10[[email protected] shell]# echo ${b[@]/7/77}    #只在显示结果里替换元素 数组不变1 2 3 4 5 6 77 8 9 10[[email protected] shell]# echo ${b[@]}1 2 3 4 5 6 7 8 9 10
[[email protected] shell]# b=(${b[@]/8/888})   #改变数组组成元素[[email protected] shell]# echo ${b[@]}1 2 3 4 5 6 7 888 9 10

Arrays in the shell

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.