Learn Bash Scripting

Bash Basics

Chapter #5: Using Arrays in Bash

Time to use arrays in bash shell scripts in this chapter. Learn to add elements, delete them and get array length.

In the earlier part of the series, you learned about variables. The variables can have a single value in it.

Arrays can have several values inside it. This makes things easier when you have to deal with several variables at a time. You don't have to store individual values in a new variable.

So, instead of declaring five variables like this:

distro1=Ubuntu
distro2=Fedora
distro3=SUSE
distro4=Arch Linux
distro5=Nix

You can initialize all of them in a single array:

distros=(Ubuntu Fedora SUSE "Arch Linux" Nix)

Unlike some other programming languages, you don't use commas as array element separators.

That's good. Let's see how to access the array elements.

Accessing array elements in bash

The array elements are accessed using the index (position in the array). To access array element at index N, use:

${array_name[N]}
πŸ’‘
Like most other programming languages, the array starts at index 0 in Bash shell. This means the first element has index 0, the second element has index 1 and the nth element has index n-1.

So, if you want to print the SUSE, you'll use:

echo ${distros[2]}
Example of accessing array elements in bash shell
🚧
There must not be any white space after ${ or before }. You CANNOT use it like ${ array[n] }.

Access all array elements at once

Let's say you want to print all the elements of an array.

You may use echo ${array[n]} one by one but that's really not necessary. There is a better and easier way:

${array[*]}

That will give you all the array elements.

Accessing all array elements at once in bash shell

Get array length in bash

How do you know how many elements are there in an array? There is a dedicated way to get array length in Bash:

${#array_name[@]}

That's so simple, right?

Get array length in bash

Add array elements in bash

If you have to add additional elements to an array, use the += operator to append element to existing array in bash:

array_name+=("new_value")

Here's an example:

Append new element to array
🚧
It is important to use () while appending an element.

You can also use the index to set the element at any position.

array_name[N]=new_value

But remember to use the correct index number. If you use it on an existing index, the new value will replace the element.

If you use an 'out of bound' index, it will still be added after the last element. For example, if the array length is six and you try to set a new value at index 9, it will still be added as the last element at the 7th position (index 6).

Delete an array element

You can use unset shell built-in to remove an array element by providing the index number:

unset array_name[N]

Here's an example, where I delete the 4th element of the array.

Delete array element in bash

You can also delete the entire array with unset:

unset array_name
πŸ’‘
There are no strict data type rules in Bash. You can create an array that contains integers and strings both.

πŸ‹οΈ Exercise time

Let's practice what you learned about bash arrays.

Exercise 1: Create a bash script that has an array of five best Linux distros. Print them all.

Now, replace the middle choice with Hannah Montanna Linux.

Exercise 2: Create a bash script that accepts three numbers from the user and then prints them in reverse order.

Expected output:

Enter three numbers and press enter
12 23 44
Numbers in reverse order are: 44 23 12

You can discuss your answers in the dedicated thread in the Community.

Practice Exercise in Bash Basics Series #5: Using Arrays in Bash
If you are following the Bash Basics series on It’s FOSS, you can submit and discuss the answers to the exercise at the end of the chapter: Fellow experienced members are encouraged to provide their feedback to new members. Do note that there could be more than one answer to a given problem.

I hope you are enjoying learning bash shell scripting with this series. In the next chapter, you'll learn about handling strings in bash. Stay tuned.

Bash Basics Series #6: Handling String Operations
In this chapter of the Bash Basics series, learn to perform various common string operations like extracting, replacing and deleting substrings.