How to use bash array in a shell script

In this tutorial, we will see how to use Bash arrays and perform fundamental operations on them. Bash, the Bourne Again Shell, is the default shell on practically all major Linux distributions: it is really powerful and can also be considered as a programming language, although not as sophisticated or feature-reach as Python or other “proper” languages. Furthermore, Bash scripting is a must-have skill for any Linux system administration job.

Bash arrays are primarily used inside of a shell script as an efficient way to store information. This information is stored in an indexed way, making it easy to recall throughout different parts of the script. It works just like storing information inside of variables, except that it also gives us a way to call back information using indexed and predictable numbers. We can also use arrays to create associations. If you are working with a script that is supposed to store a lot of information, Bash arrays will be an essential component to incorporate.

In this tutorial you will learn how to:

  • Create bash array
  • Print values from bash array
  • Obtain bash array size
  • Add and delete bash array elements

 

How to use bash array in a shell script
How to use bash array in a shell script

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any GNU/Linux distribution
Software Bash interpreter
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

After following this tutorial you should be able to understand how bash arrays work and how to perform the basic operations on them.

Create an array

The first thing we should do is distinguish between a Bash indexed array and a Bash associative array. The former are arrays in which the keys are ordered integers, while the latter are arrays in which the keys are represented by strings. Although indexed arrays can be initialized in many ways, associative ones can only be created by using the declare command as we will see in a moment.



Create indexed or associative arrays by using declare

We can explicitly create an array by using the declare command:

$ declare -a my_array

Declare, in bash, it’s used to set variables and attributes. In this case, since we provided the -a option, an indexed array has been created with the my_array name.

Associative arrays can be created in the same way: the only thing we need to change is the option used: instead of lowercase -a we must use the -A option of the declare command:

$ declare -A my_array

This, as already said, it’s the only way to create associative arrays in bash.

Create indexed arrays on the fly

We can create indexed arrays with a more concise syntax, by simply assign them some values:

$ my_array=(foo bar)

In this case we assigned multiple items at once to the array, but we can also insert one value at a time, specifying its index:

$ my_array[0]=foo

Bash Array operations

Once an array is created, we can perform some useful operations on it. For example, like displaying its keys and values or modifying it by appending or removing elements:

Print the values of an array

To display all the values of an array we can use the following shell expansion syntax:

$ echo ${my_array[@]}

Or even:

$ echo ${my_array[*]}

Both syntax let us access all the values of the array and produce the same results, unless the expansion it’s quoted. In this case a difference arises: in the first case, when using @, the expansion will result in a word for each element of the array. This becomes immediately clear when performing a for loop. As an example, imagine we have an array with two elements, “foo” and “bar”:

$ my_array=(foo bar)

Performing a for loop on it will produce the following result:

$ for i in "${my_array[@]}"; do echo "$i"; done
foo
bar

When using *, and the variable is quoted, instead, a single “result” will be produced, containing all the elements of the array:

$ for i in "${my_array[*]}"; do echo "$i"; done
foo bar




Print the keys of a bash array

It’s even possible to retrieve and print the keys used in an indexed or associative array, instead of their respective values. The syntax is almost identical, but relies on the use of the ! operator:

$ my_array=(foo bar baz)
$ for index in "${!my_array[@]}"; do echo "$index"; done
0
1
2

The same is valid for associative arrays:

$ declare -A my_array
$ my_array=([foo]=bar [baz]=foobar)
$ for key in "${!my_array[@]}"; do echo "$key"; done
baz
foo

As you can see, being the latter an associative array, we can’t count on the fact that retrieved values are returned in the same order in which they were declared.

Getting the size of an bash array

We can retrieve the size of an array (the number of elements contained in it), by using a specific shell expansion:

$ my_array=(foo bar baz)
$ echo "the array contains ${#my_array[@]} elements"
the array contains 3 elements
Retrieve bash array size
Retrieve bash array size

We have created an array which contains three elements, “foo”, “bar” and “baz”, then by using the syntax above, which differs from the one we saw before to retrieve the array values only for the # character before the array name, we retrieved the number of the elements in the array instead of its content.

Adding elements to an bash array

As we saw, we can add elements to an indexed or associative array by specifying respectively their index or associative key. In the case of indexed arrays, we can also simply add an element, by appending to the end of the array, using the += operator:

$ my_array=(foo bar)
$ my_array+=(baz)

If we now print the content of the array we see that the element has been added successfully:

$ echo "${my_array[@]}"
foo bar baz

Multiple elements can be added at a time:

$ my_array=(foo bar)
$ my_array+=(baz foobar)
$ echo "${my_array[@]}"
foo bar baz foobar

To add elements to an associative array, we are bound to specify also their associated keys:

$ declare -A my_array

# Add single element
$ my_array[foo]="bar"

# Add multiple elements at a time
$ my_array+=([baz]=foobar [foobarbaz]=baz)


Deleting an element from the bash array

To delete an element from the array we need to know it’s index or its key in the case of an associative array, and use the unset command. Let’s see an example:

$ my_array=(foo bar baz)
$ unset my_array[1]
$ echo ${my_array[@]}
foo baz

We have created a simple array containing three elements, “foo”, “bar” and “baz”, then we deleted “bar” from it running unset and referencing the index of “bar” in the array: in this case we know it was 1, since bash arrays start at 0. If we check the indexes of the array, we can now see that 1 is missing:

$ echo ${!my_array[@]}
0 2

The same thing it’s valid for associative arrays:

$ declare -A my_array
$ my_array+=([foo]=bar [baz]=foobar)
$ unset my_array[foo]
$ echo ${my_array[@]}
foobar

In the example above, the value referenced by the “foo” key has been deleted, leaving only “foobar” in the array.

Deleting an entire array, it’s even simpler: we just pass the array name as an argument to the unset command without specifying any index or key:

$ unset my_array
$ echo ${!my_array[@]}

After executing unset against the entire array, when trying to print its content an empty result is returned: the array doesn’t exist anymore.

Conclusions

In this tutorial, we learned how to use Bash arrays in a shell script. We also saw the difference between indexed and associative arrays in bash. Next, we learned how to initialize them and how to perform fundamental operations, like displaying their keys and values and appending or removing items. Finally, we saw how to unset them completely. Bash syntax can sometimes be pretty weird, but using arrays in scripts can be really useful. When a script starts to become more complex than expected, my advice is to switch to a more capable scripting language such as Python.



Comments and Discussions
Linux Forum