Bash 2.x provides the ability to create one-dimensional arrays.
- There are several ways to create, using the built-in command declare-a or direct array elements to assign values.
- When assigning a value to an array, if you do not specify a subscript, the subscript automatically starts at 0 and increments by 1 each time.
- There is no limit to the size of the array, and the subscripts do not have to be numbers in a certain order.
- The syntax for getting an array element is:${arrayname[index]}
- The syntax for getting a list of all array elements is:${arrayname[*]}
- The syntax for getting the number of array elements is:${#arrayname [*]}
$declare-A nums= (45 33 100 65)
$echo ${nums[0]}
45
$echo ${nums[*]}
45 33 100 65
$states = (ME [3]=CA [2]=CT]
$echo ${states[*]}
ME CA CT
$names = (Tom Peter Mike)
$names [5]=panda
$echo "All the array elements is ${names[*]}"
All the array elements is Tom Peter Mike Panda
$echo "The number of elements in the array is ${#names [*]}"
The number of elements in the array is 4
$unset names
=-=-=-=-=
Powered by Blogilo
An array of bash