Linux Shell array creation and usage tips

Source: Internet
Author: User

Linux Shell is much more powerful than Windows batch processing in programming, whether in loops or operations. Data Types cannot be compared. The following is a summary of some operations on the array when I am using it.

 

1. array Definition

 

[Chengmo @ centos5 ~] $ A = (1 2 3 4 5)
[Chengmo @ centos5 ~] $ Echo $
1

 

An array is represented by a pair of parentheses, And the array elements are separated by spaces.

 

2. array reading and assignment

  • Get Length:

[Chengmo @ centos5 ~] $ Echo $ {# A [@]}
5

Use $ {# array name [@ or *]} to get the array length.

  • Read:

[Chengmo @ centos5 ~] $ Echo $ {A [2]}
3

[Chengmo @ centos5 ~] $ Echo $ {A [*]}
1 2 3 4 5

Use $ {array name [subscript]} as the subscript starting from 0: * or @ to get the entire array content

  • Assignment:

[Chengmo @ centos5 ~] $ A [1] = 100

[Chengmo @ centos5 ~] $ Echo $ {A [*]}
1 100 3 4 5

 

[Chengmo @ centos5 ~] $ A [5] = 100
[Chengmo @ centos5 ~] $ Echo $ {A [*]}

1 100 3 4 5 100

The array name [subscript] can be used to reference and assign values. If the subscript does not exist, a new array element is automatically added.

  • Delete:

[Chengmo @ centos5 ~] $ A = (1 2 3 4 5)
[Chengmo @ centos5 ~] $ Unset
[Chengmo @ centos5 ~] $ Echo $ {A [*]}

[Chengmo @ centos5 ~] $ A = (1 2 3 4 5)
[Chengmo @ centos5 ~] $ Unset A [1]
[Chengmo @ centos5 ~] $ Echo $ {A [*]}
1 3 4 5
[Chengmo @ centos5 ~] $ Echo $ {# A [*]}
4

You can use the unset array [subscript] to clear the corresponding elements without any subscript.

 

 

3. Special Use

  • Parts:

[Chengmo @ centos5 ~] $ A = (1 2 3 4 5)
[Chengmo @ centos5 ~] $ Echo $ {A [@]: 0: 3}
1 2 3
[Chengmo @ centos5 ~] $ Echo $ {A [@]: 1: 4}
2 3 4 5

[Chengmo @ centos5 ~] $ C = ($ {A [@]: 1: 4 })
[Chengmo @ centos5 ~] $ Echo $ {# C [@]}
4
[Chengmo @ centos5 ~] $ Echo $ {C [*]}
2 3 4 5

Use $ {array name [@ or *]: Start position: length} to segment the original array. The returned value is a string separated by a space. Therefore, if you add "()", the Slice array is obtained. For example, C is a new data.

  • Replace:

[Chengmo @ centos5 ~] $ A = (1 2 3 4 5)
[Chengmo @ centos5 ~] $ Echo $ {A [@]/3/100}
1 2 100 4 5
[Chengmo @ centos5 ~] $ Echo $ {A [@]}
1 2 3 4 5
[Chengmo @ centos5 ~] $ A = ($ {A [@]/3/100 })
[Chengmo @ centos5 ~] $ Echo $ {A [@]}
1 2 100 4 5

The call method is $ {array name [@ or *]/find character/replace character}. This operation does not change the original array content. If you need to modify it, refer to the example above, redefine data.

As mentioned above, we can find that the array of Linux Shell is very powerful, and common operations are more than enough.

Linux Shell is much more powerful than Windows batch processing in programming, whether in loops or operations. Data Types cannot be compared. The following is a summary of some operations on the array when I am using it.

 

1. array Definition

 

[Chengmo @ centos5 ~] $ A = (1 2 3 4 5)
[Chengmo @ centos5 ~] $ Echo $
1

 

An array is represented by a pair of parentheses, And the array elements are separated by spaces.

 

2. array reading and assignment

  • Get Length:

[Chengmo @ centos5 ~] $ Echo $ {# A [@]}
5

Use $ {# array name [@ or *]} to get the array length.

  • Read:

[Chengmo @ centos5 ~] $ Echo $ {A [2]}
3

[Chengmo @ centos5 ~] $ Echo $ {A [*]}
1 2 3 4 5

Use $ {array name [subscript]} as the subscript starting from 0: * or @ to get the entire array content

  • Assignment:

[Chengmo @ centos5 ~] $ A [1] = 100

[Chengmo @ centos5 ~] $ Echo $ {A [*]}
1 100 3 4 5

 

[Chengmo @ centos5 ~] $ A [5] = 100
[Chengmo @ centos5 ~] $ Echo $ {A [*]}

1 100 3 4 5 100

The array name [subscript] can be used to reference and assign values. If the subscript does not exist, a new array element is automatically added.

  • Delete:

[Chengmo @ centos5 ~] $ A = (1 2 3 4 5)
[Chengmo @ centos5 ~] $ Unset
[Chengmo @ centos5 ~] $ Echo $ {A [*]}

[Chengmo @ centos5 ~] $ A = (1 2 3 4 5)
[Chengmo @ centos5 ~] $ Unset A [1]
[Chengmo @ centos5 ~] $ Echo $ {A [*]}
1 3 4 5
[Chengmo @ centos5 ~] $ Echo $ {# A [*]}
4

You can use the unset array [subscript] to clear the corresponding elements without any subscript.

 

 

3. Special Use

  • Parts:

[Chengmo @ centos5 ~] $ A = (1 2 3 4 5)
[Chengmo @ centos5 ~] $ Echo $ {A [@]: 0: 3}
1 2 3
[Chengmo @ centos5 ~] $ Echo $ {A [@]: 1: 4}
2 3 4 5

[Chengmo @ centos5 ~] $ C = ($ {A [@]: 1: 4 })
[Chengmo @ centos5 ~] $ Echo $ {# C [@]}
4
[Chengmo @ centos5 ~] $ Echo $ {C [*]}
2 3 4 5

Use $ {array name [@ or *]: Start position: length} to segment the original array. The returned value is a string separated by a space. Therefore, if you add "()", the Slice array is obtained. For example, C is a new data.

  • Replace:

[Chengmo @ centos5 ~] $ A = (1 2 3 4 5)
[Chengmo @ centos5 ~] $ Echo $ {A [@]/3/100}
1 2 100 4 5
[Chengmo @ centos5 ~] $ Echo $ {A [@]}
1 2 3 4 5
[Chengmo @ centos5 ~] $ A = ($ {A [@]/3/100 })
[Chengmo @ centos5 ~] $ Echo $ {A [@]}
1 2 100 4 5

The call method is $ {array name [@ or *]/find character/replace character}. This operation does not change the original array content. If you need to modify it, refer to the example above, redefine data.

As mentioned above, we can find that the array of Linux Shell is very powerful, and common operations are more than enough.

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.