Linux Shell Array Common operation detailed

Source: Internet
Author: User

Linux Shell Array Common operation detailed

1 Array Definitions:

Declare-a array Name

Array name = (element 1 element 2 element 3)

1 Declare-a array2 array= (12345)

Arrays are enclosed in parentheses, and the array elements are separated by a space

2 Display Array Length:

Echo ${#array [@]} 5  Echo ${#array [*]}5

Command:

${#数组名 [@ or *]} to get the array length, if the array has no elements, the output is empty

3 Reading array elements:

[@tc_132_227 dm_pid_day]$Echo${array[0]}1[@tc_132_227 dm_pid_day]$Echo${array[1]}2[@tc_132_227 dm_pid_day]$Echo${array[4]}5[@tc_132_227 dm_pid_day]$Echo${array[*]}1 2 3 4 5[@tc_132_227 dm_pid_day]$Echo${array[@]}1 2 3 4 5

Command:
${array name [subscript]}, array subscript starting from 0, and subscript as * or @ symbol, reading the entire array element

4 assigning values to array elements:

[@tc_132_227 dm_pid_day]$Echo${array[@]}1 2 3 4 5[@tc_132_227 dm_pid_day]$ array[1]=9[@tc_132_227 dm_pid_day]$Echo${array[@]}1 9 3 4 5[@tc_132_227 dm_pid_day]$ array[8]=7[@tc_132_227 dm_pid_day]$Echo${array[@]}1 9 3 4 5 7[@tc_132_227 dm_pid_day]$

Command:

Array name [subscript]= element Assignment If the subscript does not exist, the array will automatically add a new element

5 Deleting an array element:

[@tc_132_227 dm_pid_day]$ unset array[0[@tc_132_227 dm_pid_day]$Echo${array[@]}2 3 4 5[@tc_132_227 dm_pid_day]$Echo${#array [*]}4[@tc_132_227 dm_pid_day]$ unset array[1[@tc_132_227 dm_pid_day]$Echo${array[*]}3 4 5[@tc_132_227 dm_pid_day]$Echo${#array [*]}3[@tc_132_227 dm_pid_day]$ unset array[@tc_132_227 dm_pid_day]$Echo${array[*]}[@tc_132_227 dm_pid_day]$

Command:

unset array name [subscript] to delete array element, unset array name to delete entire array

6 Array Shards:

[@tc_132_227 dm_pid_day]$Echo${array[@]}1 2 3 4 5[@tc_132_227 dm_pid_day]$Echo${array[@]:0:1}1[@tc_132_227 dm_pid_day]$Echo${array[@]:1:1}2[@tc_132_227 dm_pid_day]$Echo${array[@]:1:3}2 3 4
[@tc_132_227 dm_pid_day]$ arr= (${array[@]:1:3echo  ${arr[@]}2  34echo  ${#arr [@]}3[@tc_132_227 dm_pid_day]$ 

Command:

${array name [@|*]: Start subscript: Length} shards the original array and returns a string with a "space" delimiter, and if you add "()" to the string, you get a new shard array

Linux Shell Array Common operation detailed

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.