2.2-shell Array

Source: Internet
Author: User

A= (1 2 3 4 5) define an array

echo $a show only 1 and cannot display all elements of the entire array

echo ${a[@]} displays all elements of the entire array, and @ can also be written as *

echo ${a[0]} displays only the first element in the array, marking 0 If the print does not exist for tag 5, the display is empty.


A[5]=6 adds an element and tags to the array

a[2]=7 Modifying the element of marker 2 to 7

echo ${#a [@]} in the print array, a total of several elements

unset a[2] within the array, delete an element and tag



Application of the array: sort 10 random numbers.

#!/bin/bash

For i in ' seq 0 9 '

Do

a[$1]= $RANDOM Assigning a value to an array

Done

echo ${a[@]}|sed ' s//\ n/g ' |sort-n after array processing, sort print \ n is a line break

Where $random is a randomly generated 3-5-bit number,sort is sorted on multiple lines, so use sed to handle whitespace as a newline


Echo $[a[@]:0:4] : 0 indicates starting from the 0 tag: 4 means displaying 4 elements


2.2-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.