Site icon DesignLinux

Bash Arrays

Bash Arrays

Arrays are set of data and fundamental requirement for any programming language. We can say that it’s a variable with multiple child variables. This article shows you basics and use of arrays in Bash scripts.

Bash Arrays

One dimensional array with numbered index and associative array types supported in Bash. Those are referenced using integers and associative are referenced using strings. Bash does not support multidimensional arrays

To access the last element of a numeral indexed array use the negative indices. The index of -1 references the last element. The indices do not have to be contiguous.

In bash, array elements can any of data type. You also can create an array that have both numbers and strings. You can store any number of element in array, as there is not maximum limit of elements.

Create Bash Arrays

In bash, you can create arrays with multiple ways.

Create numerically indexed arrays

You can create indexed array without declaring it using any variable. To explicitly declare an array, use the declare builtin:

declare -a array_name

Following is the first method to create an indexed array:

array_name[INX_1]=VAL_1
array_name[INX_2]=VAL_2
array_name[INX_3]=VAL_3

Here, INX_* is a positive integer.

The second method to create a numeric array by specify the list of the elements within parentheses, separated by empty space:

array_name=( ELE_1 ELE_2 ELE_N )

In this method the index of an array starts with zero that means the first element have an index of 0.

Create associative arrays

You must declare the associative array before they can be used. Use the built-in with the -A (uppercase) option to declare an associative array :

declare -A array_name

You can create the associative array using the following form:

declare -A array_name

array_name[inx_a]=value_a
array_name[inx_b]=value_b
array_name[inx_c]=value_c

Where inx_* can be any string.

Following is an alternate form to create an associative array:

declare -A array_name

array_name=( 
	[inx_a]=value_a
	[inx_b]=value_b
	[inx_c]=value_c
)

Array Operations

The syntax for the bash looks little bit odd than other programming language.

Reference Elements

Same as other language, to reference a single element, you should have the element index.

Any element can be referenced using the following syntax:

${array_name[index]}

For example, to print the element with index of 2:

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )
echo ${state_array[2]}
Ohio

To print the all elements of an array you would use @ or * as an index. Following form can be used to print all elements:

## DECLARATION OF ARRAY
declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

## PRINT ALL ELEMENTS
echo "${state_array[@]}"
California Texas Ohio Nevada

If you need to print keys of an array just add the ! operator before the array name:

${!array_name[index]}

Below is the example:

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )
print all elements
echo "${!state_array[@]}"
0 1 2 3

Array Length

To get the length of an array, use the following form:

${#array_name[@]}
declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

echo ${#state_array[@]}
4

Loop through the array

You can iterate over each item in an array using the for loop:

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

## Array Loop
for i in "${state_array[@]}"
do 
  echo "$i"
done

It will print each element in a new line:

California
Texas
Ohio
Nevada

To print all keys and values using following code:

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

for i in "${!state_array[@]}"
do
  echo "$i" "${state_array[$i]}"
done
0 California
1 Texas
2 Ohio
3 Nevada

Alternate way to loop an array using C style loop:

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

length=${#my_array[@]}

for (( i=0; i < ${length}; i++ ))
do
  echo $i ${my_array[$i]}
done
0 California
1 Texas
2 Ohio
3 Nevada

Add a new element

You should use the following form to add a new element to a bash array by specifying its index:

array_name[index_n]="New Element"

For example:

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

## ADDING NEW ELEMENT
state_array[7]="Arkansas"

## PRINT ALL ELEMENTS
echo "${state_array[@]}"
California Texas Ohio Nevada Arkansas

You can add a new element without specifying the index by using the += operator.

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

## ADDING NEW ELEMENT
state_array+=( Arizona Colorado )

## PRINT ALL ELEMENTS
echo "${state_array[@]}"
California Texas Ohio Nevada Arizona Colorado

Delete an element

You can use the unset command to remove a element from an array. You should have the index of an element to delete it.

unset my_array[index]

For instance,

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

unset state_array[2]

echo "${state_array[@]}"
California Texas Nevada

Conclusion

In this guide, you learned how to create an array, get array length and add or remove elements from array.

If you have any questions or feedback, please leave comment below.

Exit mobile version