How to understand Bash Arrays.

How To Understand Bash Arrays.

Nexonhost Dedicated Servers , Virtual Servers

Bash Arrays

Bash supports one-dimensional numerically indexed and associative arrays types. Numerical arrays are referenced using integers, and associative are referenced using strings.

Numerically indexed arrays can be accessed from the end using negative indices, the index of -1 references the last element. The indices do not have to be contiguous.

Unlike most of the programming languages, Bash array elements don’t have to be of the same data type. You can create an array that contains both strings and numbers.

Bash does not support multidimensional arrays, and you can’t have array elements that are also arrays.

There is no limit on the maximum number of elements that can be stored in an array.

 

Creating Bash Arrays

Arrays in Bash can be initialized in different ways.

 

Creating numerically indexed arrays

Bash variables are untyped, any variable can be used as an indexed array without declaring it.

To explicitly declare an array, use the declare builtin:

  • declare -a array_name

One way to create an indexed array is by using the following form:

  • array_name[index_1]=value_1
  • array_name[index_2]=value_2
  • array_name[index_n]=value_n

Where index_* is a positive integer.

Another way to create a numeric array is to specify the list of the elements within parentheses, separated by empty space:

  • array_name=( element_1 element_2 element_N )

When the array is created using the form above, indexing starts at zero i.e. the first element have an index of 0.

 

Creating associative arrays

Unlike numerically indexed, the associative arrays must be declared before they can be used.

To declare an associative array use the declare builtin with the -A (uppercase) option:

  • declare -A array_name

Associative arrays can be created using the following form:

  • declare -A array_name
  • array_name[index_foo]=value_foo
  • array_name[index_bar]=value_bar
  • array_name[index_xyz]=value_xyz

Where index_* can be any string.

You can also create an associative array using the form below:

  • declare -A array_name
  • array_name=(
  • [index_foo]=value_foo
  • [index_bar]=value_bar
  • [index_xyz]=value_xyz
  • )

 

Array Operations

Bash arrays syntax may look a little strange at first, but it will make more sense once you read this article.

 

Reference Elements

To reference a single element, you need to know the element index.

Any element can be referenced using the following syntax:

  • ${array_name[index]}

The syntax for accessing an array element is similar to the syntax of most of the programming languages. The curly braces ${} are required to avoid shell’s filename expansion operators.

Let’s print the element with index of 1:

  • ## declare the array
  • declare -a my_array=( “Smells Like Teen Spirit – Nirvana.”
  • “Imagine – John Lennon.”
  • “One – U2.”
  • “I Can’t Get No Satisfaction – Rolling Stones.” )
  • ## print element
  • echo ${my_array[3]}
  • Output:
  • I Can’t Get No Satisfaction – Rolling Stones.

If you use @ or * as an index, the word expands to all members of the array. To print all elements you would use:

  • ## declare the array
  • declare -a my_array=( “Smells Like Teen Spirit – Nirvana.”
  • “Imagine – John Lennon.”
  • “One – U2.”
  • “I Can’t Get No Satisfaction – Rolling Stones.” )
  • ## print all elements
  • echo “${my_array[@]}”
  • Output:
  • “Smells Like Teen Spirit – Nirvana.”
  • “Imagine – John Lennon.”
  • “One – U2.”
  • “I Can’t Get No Satisfaction – Rolling Stones.”

The only difference between @ and * is when the form ${my_array[x]} is surrounded with double-quotes. In this case, * expands to a single word where array elements are separated with space. @ expands each array element to a separate word. This is especially important when using the form to illiterate through array elements.

To print the keys of the array add the ! operator before the array name:

  • ${!array_name[index]}

Here is an example:

  • ## declare the array
  • declare -a my_array=( “Smells Like Teen Spirit – Nirvana.”
  • “Imagine – John Lennon.”
  • “One – U2.”
  • “I Can’t Get No Satisfaction – Rolling Stones.” )
  • ## print all elements
  • echo “${!my_array[@]}”
  • 0 1 2 3

 

Array Length

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

  • ${#array_name[@]}

The syntax is the same as when referencing all elements wit addition of the # character before the array name.

  • ## declare the array
  • declare -a my_array=( “Smells Like Teen Spirit – Nirvana.”
  • “Imagine – John Lennon.”
  • “One – U2.”
  • “I Can’t Get No Satisfaction – Rolling Stones.” )
  • ## array Length
  • echo ${#my_array[@]}
  • 4

 

Loop through the array

The most common way to iterate over each item in an array is by using the for loop:

  • declare -a my_array=( “Smells Like Teen Spirit – Nirvana.”
  • “Imagine – John Lennon.”
  • “One – U2.”
  • “I Can’t Get No Satisfaction – Rolling Stones.” )
  • ## Array Loop
  • for i in “${my_array[@]}”
  • do
  • echo “$i”
  • done

The code above will iterate over the array and print each element in a new line:

  • Smells Like Teen Spirit – Nirvana.
  • Imagine – John Lennon.
  • One – U2.
  • I Can’t Get No Satisfaction – Rolling Stones.

Here is an example of how to print all keys and values:

  • declare -a my_array=( “Smells Like Teen Spirit – Nirvana.”
  • “Imagine – John Lennon.”
  • “One – U2.”
  • “I Can’t Get No Satisfaction – Rolling Stones.” )
  • ## Array Loop
  • for i in “${!my_array[@]}”
  • do
  • echo “$i” “${my_array[$i]}”
  • done
  • 0 Smells Like Teen Spirit – Nirvana.
  • 1 Imagine – John Lennon.
  • 2 One – U2.
  • 3 I Can’t Get No Satisfaction – Rolling Stones.

Another way to loop through an array is to get the length of the array and use the C style loop:

  • declare -a my_array=( “Smells Like Teen Spirit – Nirvana.”
  • “Imagine – John Lennon.”
  • “One – U2.”
  • “I Can’t Get No Satisfaction – Rolling Stones.” )
  • # Length of the array
  • length=${#my_array[@]}
  • # Array Loop
  • for (( i=0; i < ${length}; i++ ))
  • do
  • echo $i ${my_array[$i]}
  • done
  • 0 Smells Like Teen Spirit – Nirvana.
  • 1 Imagine – John Lennon.
  • 2 One – U2.
  • 3 I Can’t Get No Satisfaction – Rolling Stones.

 

Add a new element

To add a new element to a bash array and specify its index use the following form:

  • my_array[index_n]=”New Element”

Here is an example:

  • declare -a my_array=( “Smells Like Teen Spirit – Nirvana.”
  • “Imagine – John Lennon.”
  • “One – U2.”
  • “I Can’t Get No Satisfaction – Rolling Stones.” )
  • ## add new element
  • my_array[9]=”Hey Jude – The Beatles.”
  • ## print all elements
  • echo “${my_array[@]}”
  • Smells Like Teen Spirit – Nirvana. Imagine – John Lennon. One – U2. I Can’t Get No Satisfaction – Rolling Stones. Hey Jude – The Beatles.

Another way of adding a new element to an array without specifying the index is by using the += operator. You can add one or multiple elements:

declare -a my_array=( “Smells Like Teen Spirit – Nirvana.”

  • “Imagine – John Lennon.”
  • “One – U2.”
  • “I Can’t Get No Satisfaction – Rolling Stones.” )
  • ## add new elements
  • my_array+=( Hey Jude – The Beatles. )
  • ## print all elements
  • echo “${my_array[@]}”
  • Smells Like Teen Spirit – Nirvana. Imagine – John Lennon. One – U2. I Can’t Get No Satisfaction – Rolling Stones. Hey Jude – The Beatles.

 

Delete an element

To delete a single element, you’ll need to know the element index. An element can be removed using the unset command:

  • unset my_array[index]

Let’s see an example:

  • declare -a my_array=( “Smells Like Teen Spirit – Nirvana.”
  • “Imagine – John Lennon.”
  • “One – U2.”
  • “I Can’t Get No Satisfaction – Rolling Stones.” )
  • ## remove element
  • unset my_array[2]
  • ## print all elements
  • echo “${my_array[@]}”
  • Smells Like Teen Spirit – Nirvana. Imagine – John Lennon. I Can’t Get No Satisfaction – Rolling Stones.

 

Conclusion

We’ve explain how to create numerically indexed and associative arrays. We have also show how to iterate through the arrays, calculate the array length, and add and remove elements.