There are variables in bash, which have a very important feature: only one value can be stored in a variable, but in practice, a value is often not up to our requirements, so we introduce an array: a contiguous memory space that accommodates multiple groups of elements, and its introduction makes people more productive.
The array is divided into sparse arrays and dense arrays, where bash belongs to sparse arrays. There are array elements in the array: any storage unit in the array that holds the data, which acts as a variable.
Array element identifiers fall into the following categories:
Index Array ID: All array elements are numbered using numbers;
Usually the number is 0, which is: 0,1,2,3 ...
Associative array ID: All array elements can be identified by using a name (string);
Note: It is generally possible to support associative arrays in versions above bash4.0;
The life and definition of an array:
1.declare command:
-A to make NAMEs indexed arrays (if supported)
Declare the variable name after it as an indexed array;
-A to make NAMEs associative arrays (if supported)
Declare the variable name after it as an associative array;
Example:
To define a dense array:
[Email protected] ~]# declare-a names= ("Zhao" "Qian" "Sun" "Li") [[email protected] ~]# echo ${names[3]}li
To define a sparse array:
[Email protected] ~]# declare-a names= ([0]= "Zhao" [2]= "Qian" [8]= "Sun" [10]=] li ") [[email protected] ~]# echo ${names[0]} Zhao
2. How to assign values directly using variables:
Define a dense array of indexes:
Array_name= ("value1" "value2" "value3" ...)
To define a sparse index array:
Array_name= ([0]= "value1" [1]= "value2" [3] "value3" ...)
To define an associative array:
Array_name= ([index_inanme]= ' value1 ' index_inanme= ' value2 ' ...)
3. Define the array elements separately:
array_name[0]= ' value1 '
array_name[1]= ' value2 '
array_name[2]= ' Value3 '
...
Array_name[n-1]= ' Value (n-1) '
[Email protected] ~]# student_inf[o]= ' xiaoma ' [[email protected] ~]# student_inf[1]= ' [[email protected] ~]# Student_ inf[2]= ' 123456789 ' [[email protected] ~]# echo ${student_inf[0]}xiaoma[[email protected] ~]# echo ${student_inf[1]}22[[ Email protected] ~]# echo ${student_inf[2]}123456789
How to reference array elements:
${array_name[index]}
Note: If you do not give a specific index number when referencing an array element, the default number is "0", which shows the numeric value of the first array element;
Refer to all elements in the entire array:
${array_name[*]} or ${array_name[@]}
[Email protected] ~]# student_inf[o]= ' xiaoma ' [[email protected] ~]# student_inf[1]= ' [[email protected] ~]# Student_ inf[2]= ' 123456789 ' [[email protected] ~]# echo ${student_inf[*]}xiaoma [123456789[[email protected] ~]# echo ${student_ Inf[@]}xiaoma 22 123456789
Index number of all elements referencing the entire array:
${! Array_name[*]} or ${! Array_name[@]}
[Email protected] ~]# echo ${! Student_inf[@]}0 1 2[[email protected] ~]# echo ${! Student_inf[*]}0 1 2
To view the number of elements in an array (array length):
${#ARRAY_NAME [*]} or ${#ARRAY_NAME [@]}
[[email protected] ~]# echo ${student_inf[@]}xiaoma 123456789[[email protected] ~]# echo ${#STUDENT_INF [*]}3[[email PR Otected] ~]# echo ${#STUDENT_INF [@]}3
Array slices:
${array_name[*]:offset} or ${array_name[@]:offset}
Displays the element that contains the offset value and all subsequent elements thereof;
[[email protected] ~]# echo ${names[@]}zhao Qian Sun Li[[email protected] ~]# echo ${names[@]:2}qian Sun Li
${array_name[*]:offset:number} or ${array_name[@]:offset:number}
Displays the value of the element in the array that contains the position where the offset value corresponds and the number of its number elements;
[[email protected] ~]# echo ${names[@]}zhao Qian Sun Li[[email protected] ~]# echo ${names[@]:2:2}qian Sun
Undo Array:
Unset Array_Name
Random variable: Location:/dev/random
Random number variable: 0-32767, integer value;
[[email protected] ~]# echo $RANDOM 383[[email protected] ~]# echo $RANDOM 30783
How random numbers are generated:
Taking random numbers from the entropy pool;
Entropy Pool:
/dev/random
Two times the time interval of hitting the keyboard;
Two time interval of IO;
...
/dev/uradom: Pseudo-entropy pool
Calculate the random number by using the application;
The array in bash