Methods for creating sequences and Arrays (list, array) in a shell _linux shell

Source: Internet
Author: User

For Linux array definitions, and build methods, see: Linux shell dynamically generate array series SEQ usage tips. My main point here is to efficiently generate the list string, as well as the array method.

One, the SEQ method produces:

[Chengmo@centos5 shell]$ anumlist=$ (seq);
[Chengmo@centos5 shell]$ Echo $aNumList
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27  30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Anumlist get is a string, different penalty: the space separates. In Linux, you can think of it as a list. Can be read through for...in loops.

[Chengmo@centos5 shell]$ for i $aNumList;d o echo $i;d one;
1
2
3
4 ...

If you need to generate an array, just add the $ (SEQ 100) to "()".

[Chengmo@centos5 ~]$ anumlist= ($ (SEQ));
[Chengmo@centos5 ~]$ Echo $aNumList
1
[chengmo@centos5 ~]$ echo ${#aNumList [@]}
100

An array with a length of 100.

Second, through the internal {begin ... End} Build

This method is very handy for generating seq. Done through an internal operator.

[Chengmo@centos5 ~]$ echo {1..10}
1 2 3 4 5 6 7 8 9

[Chengmo@centos5 ~]$ for a in {1..10};d o echo $a;d one; 
1
2
3
4
5
6 7 8 9 10

Third, performance comparison

[Chengmo@centos5 ~]$ time echo {1..100}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28  30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 The 0m0.000s of the Yi-An-yi-an-yi-yi------  0m0.001s
sys   0m0.000s

 

[chengmo@centos5 ~]$ time echo $ (seq)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 6 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54  57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The real  0m0.003s
user  0m0.002s
sys   0m0.001s

As you can see from above, {begin ... The end} speed is much faster than the SEQ call. Later calls can be considered to be done through the internal operator.

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.