Arrays in shells are variable to hold more than one value.
Suppose, You have a list of numbers 1 2 3.. 10 and want to store these numbers in Shell Script
Without arrays, You have to declare as follows
let number1=1
let number1=1
...
...
let number10=10
Iteration is difficult and if we want to store 100 numbers, It is very difficult.
So, You can use an array referring to a single variable and store it.
How to declare and create an array?
There are two types of arrays we can create
indexed arrays: array elements are stored with the index starting from zero
associated arrays: array is stored with key-value pairs
Declare an array
To create an array, We need to declare an array.
declares -a array; # indexed array
declare -A array; # associative array
an array is declared with the keyword declare
with option -a
or A
indexed array example
In this, Array values are stored with index=0 onwards. these are created with declare
and -a
option
declare -a array
array=(one two three)
This array is a store with index=0, incremented by 1 as follows.
array[0]=one
array[1]=two
array[2]=three
associative array example
In this, Array values are stored with keys. these are created with declare
and -A
option
declare -A array
array=(one two three)
In this array is a store with index=0, incremented by 1 as follows
array[key1]=one
array[key2]=two
array[key3]=three
Let’s assign the values.
array=(1,2,3,4)
Assign the values without declaring an array
arrayvariable[index]=value
This means, that
arrayvariable
is declared and assigned an array index with value.
Arrays are zero-indexed based on zero to the length of an array -1 index=0 - returns the first element index=-1 returns the last element
Arrays can contain numbers, strings, and a mix of it Let’s create an array of examples.
Access the array values
An array contains an index to get elements. Array elements can be accessed using the below syntax.
${array_name[index]}
Declare an Array of numbers and loop through
Arrays can contain numbers This example contains an array of numbers and for loop to print
nums=(1 3 12)
for i in "${nums[@]}"
do
echo "$i"
done
Output:
1
3
12
Declare an Array of strings and loop through
Arrays can contain numbers This example contains an array of numbers and for loop to print
numbers=("element1" "element2" "element3")
for i in "${numbers[@]}"
do
echo "$i"
done
Output:
element1
element2
element3
Access the first elements of an array
In Array elements, the First element index is zero, and array[0] returns the first element
numbers=("element1" "element2" "element3")
echo ${numbers[0]}
echo ${numbers}
Output:
element1
element1
Get the last element of an array
In a bash script, you can use index=-1 to get the last array element.
numbers=("element1" "element2" "element3")
echo ${numbers[-1]}
With the recent bash 4.0 version, you can use the below syntax to read the last element.
echo ${numbers[${#numbers[@]}-1]}
Iterate or loop array elements
For loop is used to iterate elements.
Here is an example loop array example to print all elements
numbers=("element1" "element2" "element3")
for i in "${numbers[@]}"
do
echo "$i"
done
Output:
element1
element2
element3
Another way to print the index and elements of an array using for loop.
numbers=("element1" "element2" "element3")
for i in "${!numbers[@]}"
do
echo "$i" "${numbers[$i]}"
done
Output:
0 element1
1 element2
2 element3
Print all array elements
Use [@] or [*] to print all elements of an array.
arr=("element1" "element2" "element3") //
echo ${arr[@]} #element1 element2 element3
echo ${arr[*]} #element1 element2 element3
Remove an element from an array
You can remove an element from an array using unset
for a given index.
numbers=("element1" "element2" "element3")
echo ${numbers[*]}
unset numbers[-1]
echo ${numbers[*]}
Adding an element to an array
You can add an element at any index position using the below syntax.
array[index]=value
An example of adding elements starting and end as well as middle
numbers=("element1" "element2" "element3")
echo ${numbers[*]}
numbers[0]="element0"
echo ${numbers[*]}
numbers[5]="element5"
echo ${numbers[*]}
numbers[6]="element6"
echo ${numbers[*]}
Output:
element1 element2 element3
element0 element2 element3
element0 element2 element3 element5
element0 element2 element3 element5 element6
Length of an array
In this, Find the count of all elements in an array.
Shell script provides #
arr=("element1" "element2" "element3")
echo ${#arr[@]} # returns 3
echo ${#arr[*]} # returns 3
Array cheat sheet examples
Example | Description |
---|---|
declare -a array | Declare an Indexed array |
declare -A array | Declare an Associative array |
declare -a array=() | Declare an indexed array with empty array |
array=() | create an empty array with declaring is valid |
array=(1 6 3) | Initialize array with numbers |
array=(one two three) | Initialize the array with string |
array=(one two 1) | Initialize the array with mixed data |
${array[0]} | Get first element |
${array[1]} | Get Second element |
${array[-1]} | Get Last element |
${array[@]} | Get All elements |
${array[*]} | Get All elements |
${!array[!]} | Get All indexes |
${#array[!]} | Array length |
array[0]=12 | Add element to array at first position.i.e index=0 |
array[-1]=22 | Add element to array at last position. |
array+=(11) | Append value to an array |
${array[@]:k:i} Get index=1 element starting from index=k |