How to define Shell array
This article introduces the relevant knowledge of "how to define Shell array". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Bash supports one-dimensional arrays (not multidimensional arrays) and does not limit the size of the array. Similar to the C language, the subscripts of array elements are numbered starting with 0. The subscript is used to get the elements in the array, which can be an integer or an arithmetic expression whose value should be greater than or equal to 0.
Define array
In Shell, arrays are represented by parentheses, and array elements are separated by a "space" symbol. The general form of defining an array is:
Array name = (value 1, value 2. Value n)
For example:
Array_name= (value0 value1 value2 value3)
Or
Array_name= (value0value1value2value3)
You can also define individual components of an array:
Array_name [0] = value0array_name [1] = value1array_ name [n] = valuen
There can be no use of consecutive subscripts, and there is no limit to the range of subscripts.
Read array
The general format for reading array element values is:
${Array name [subscript]}
For example:
Valuen=$ {array_ name[n]}
Use the @ symbol to get all the elements in the array, such as:
Echo ${array_name [@]} gets the length of the array
Get the length of an array in the same way as you get the length of a string, for example:
# get the number of array elements length=$ {# array_name [*]} # or length=$ {# array_name [*]} # get the length of a single element of the array lengthn=$ {# array_ name [n]} "how to define the array" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!