Bash provides a feature to declare a list (or array) of variables in a one-dimensional array that can be an indexed array or associative array. The size of an array can be 0
or more.
An indexed array contains variables that may or may not have been initialized continuously. Indices of an indexed array start from 0
. This means that the first element of an array will start at an index 0
.
An indexed array can be declared by just initializing any index as follows:
array_name[index]=value
Here, an index can be any positive integer or an expression must be evaluated to a positive integer.
Another way of declaring is by using the declare
shell built in as follows:
declare -a array_name
We can also initialize an array with values during a declaration. Values are enclosed within parentheses and each value is separated with a blank space as follows:
declare -a array_name=(value1 value2 value3 …)
Initializing and declaring values to a variable is not sufficient. The actual usage of an array is when we perform different operations on it to get the desired result.
The following operations can be done on an indexed array:
echo ${array_name[index]}
@
or *
:echo ${array_name[*]} echo ${array_name[@]}
$#
with the array variable:echo ${#array_name[@]} echo ${#array_name[*]}
$#
on nth index:echo ${#array_name[n]}
unset
keyword:unset array_name[index] # Removes value at index unset array_name # Deletes entire array
The following shell script demonstrates the different operations on an indexed array:
#!/bin/bash # Filename: indexed_array.sh # Description: Demonstrating different operations on indexed array #Declaring an array conutries and intializing it declare -a countries=(India Japan Indonesia 'Sri Lanka' USA Canada) # Printing Length and elements of countries array echo "Length of array countries = ${#countries[@]}" echo ${countries[@]} # Deleting 2nd element of array unset countries[1] echo "Updated length and content of countries array" echo "Length = ${#countries[@]}" echo ${countries[@]} # Adding two more countries to array countries=("${countries[@]}" "Indonesia" "England") echo "Updated length and content of countries array" echo "Length = ${#countries[@]}" echo ${countries[@]}
The output after executing this script is as follows:
Length of array countries = 6 India Japan Indonesia Sri Lanka USA Canada Updated length and content of countries array Length = 5 India Indonesia Sri Lanka USA Canada Updated length and content of countries array Length = 7 India Indonesia Sri Lanka USA Canada Indonesia England
The associative array contains a list of elements in which each element has a key-value pair. The elements of an associative array are not referred by using an integer value 0
to N
. It is referred by providing a key name that contains a corresponding value. Each key name should be unique.
The declaration of an associative array is done by using the -A
option with the declare
shell builtin as follows:
declare -A array_name
An associate array uses a key instead of an index within a square bracket in order to initialize a value as follows:
array_name[key]=value
Multiple values can be initialized in the following way:
array_name=([key1]=value1 [key2]=value2 ...)
A few operations on an associative array can be done similar to how an indexed array does, such as printing the length and content of an array. The operations are as follows:
echo ${array_name[key]}
echo ${array_name[*]} echo ${array_name[@]} Obtaining the length of an array: echo ${#array_name[@]} echo ${#array_name[*]}
echo ${array_name[k]} # Value of key k echo ${#array_name[k]} # Length of value of key k
+=
operator as follows:array_name+=([key]=value)
k
key as follows:unset array_name[k]
array_name
as follows:unset array_name
The following shell script demonstrates the different operations on an associative array:
#!/bin/bash # Filename: associative_array.sh # Description: Demonstrating different operations on associative array # Declaring a new associative array declare -A student # Assigning different fields in student array student=([name]=Foo [usn]=2D [subject]=maths [marks]=67) # Printing length and content of array student echo "Length of student array = ${#student[@]}" echo ${student[@]} # deleting element with key marks unset student[marks] echo "Updated array content:" echo ${student[@]} # Adding department in student array student+=([department]=Electronics) echo "Updated array content:" echo ${student[@]}
The output after executing this script is as follows:
Length of student array = 4 Foo 67 maths 2D Updated array content: Foo maths 2D Updated array content: Foo maths Electronics 2D
3.21.93.139