What is Array Expansion in Bash [4 Useful Applications]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Bash shell offers multiple array handling tools and techniques. Among them array expansion is prominent. It takes the array handling capabilities to the next level. In this article, I want to demonstrate the basic syntax of array expansion as well as various practical uses of it in Bash.

Bash Array Fundamentals

An array is a collection of elements. It is a structured way to organize and access data. There are two types of arrays in Bash. Index array and associate array.

● Index Array

Indexed arrays are the most common type of arrays in Bash and are similar to arrays in many other programming languages. There are a couple of ways to declare an index array in Bash. First of all, you can initiate an indexed array using the declare with -a option.

declare -a array1=("a" "b" "c")

However, it is not mandatory to use declare explicitly to initiate an array. Rather one can use the following structure to initiate an array quickly.

array2=("d" "e" "f")

To access an element of an array put the index of the element within a square bracket after the name of the array. For instance, to get the second element of array2, use array2[1] in parameter expansion.

echo "${array2[1]}" # prints "e"

● Associate Array

Unlike an indexed array, an associate array is a collection of data with a key associated with each element of the array. To access an element of an associate array, one needs to use the key associated with that element. This is similar to the key-value pair of a Python dictionary.

You have to use the declare keyword with -A to initiate an associate array. You can define the full array in a single line like below:

declare -A array3 ( [User]=Anita [ID]=20134 [Shell]=sh )

Furthermore, you can first declare an associate array and later add an element in that array.

declare -A array4
[User]=Kelly
[ID]=20129
[Shell]=zsh

To access an element of an associate array put the key of the element within a square bracket after the name of the array. For instance, to get the value of the User key of array4, use array4[User] in parameter expansion.

echo "${array4[User]}" # prints "Kelly"

Syntax of Array Expansion in Bash

Array expansion in Bash is a kind of parameter expansion that typically refers to the process of expanding the elements of an array. “[@]” and “[*]” syntax can be used for expanding array. Both indexed and associate arrays can be expanded in this way. Let’s define a new array and explore array expansion using the aforementioned syntax.

my_arr=("a b" "1 2")

${my_arr[@]} will expand each element of the array as a separate word. Here my_arr has two elements “a b” and “1 2” respectively. However, there are four separate words in these two elements as you the see in the following image.

echo ${my_arr[@]}
# Output: a b 1 2

Syntax of array expansion${my_arr[*]} also expands each element of the array as a separate word and results in four different words. But there is a slight difference between [@]  and [*] that I will discuss later in this article.

echo ${my_arr[*]}
# Output: a b 1 2

Expanding an array as single string

NOTE: ${!array[@]} or ${!array[*]} provides the index for an indexed array and the key for an associative array, respectively
NOTE: The command unset arr[@] will unset or remove all elements from an array named arr.

Difference Between “[@]” and “[*]” in Array Expansion

You can’t differentiate between [@] and [*] unless you use these with quotes. In this demonstration, I will show you the basic difference between these two using a for loop. Let’s see the bash script to understand the difference between [@] and [*] in array expansion using for loop:

#!/bin/bash

my_arr=("a b" "1 2")
# Using [@] for array expansion
echo "Using [@]:"
for item in "${my_arr[@]}"; do
echo "$item"
done

# Using [*] for array expansion
echo "Using [*]:"
for item in "${my_arr[*]}"; do
echo "$item"
done
EXPLANATION

The script initializes an array called my_arr with two elements, “a b” and “1 2”. Then it demonstrates the difference between array expansion using [@] and [*] within two separate for loops. When using ${my_arr[@]}, each element of the array is treated as a separate word.

When using ${my_arr[*]}, the entire array is treated as a single string causing array elements to be printed on a single line.

Difference between two types of array expansionWhen ${my_arr[@]} is used for array expansion, the loop iterates over the array (“a b” “1 2”) for two iterations. It prints elements a b and 1 2 in new line.

On the contrary, when ${my_arr[]} is used, the loop iterates over the array only once, treating the entire array as a single entity and thus resulting in a b 1 2 as a single string.

4 Useful Applications of Bash Array Expansion

Array expansion serves many purposes such as slicing an array, combining multiple arrays, and counting elements within an array. It proves to be a valuable tool in various applications in Bash scripting.

1. Slicing Array to Extract Range of Elements

Array slicing is one of the major operations performed on an array. The following script will show you the way to slice an array or extract a subarray from an array. Here’s a bash script to extract a range of elements by array slicing:

#!/bin/bash

# Create an array of numbers
myArray=(10 20 30 40 50 60 70)

# Slice the array to extract elements from index 2 to 4 (inclusive)
slicedArray=("${myArray[@]:2:3}")

# Print the slicedArray
echo "${slicedArray[@]}"
EXPLANATION

This Bash script initializes an array called myArray. It then slices the array from index 2 (the third element in myArray) and includes the next 3 elements, resulting in slicedArray containing the values 30, 40, and 50.

Slicing an arrayThe program slices the array (10 20 30 40 50 60 70) from index 2 up to the next 3 items of the array.

2. Combining Multiple Arrays Into One Using Array Expansion

One can easily combine two or more arrays into a new array using Bash array expansion. [@] is used for expanding the arrays and + operator is used to add them. Check the bash script to combine multiple arrays into one using bash array expansion:

#!/bin/bash

arr1=(a b c d)
arr2=(1 2 3)
combined=("${arr1[@]}" "${arr2[@]}")  # Use arr2 here
echo "${combined[@]}"
EXPLANATION

This Bash script defines two arrays, arr1 and arr2. It then combines these arrays into a new array called combined using array expansion. The combined array contains all the elements from arr1 followed by all the elements from arr2. Finally, it prints the elements of the combined array using the echo command.

Combining two array into oneHere, arr1 contains the elements a, b and c, and arr2 contains the numbers 1, 2, and 3. The program expands both arrays and combines them. The resulting combined array a b c d 1 2 3 is displayed using the echo command.

3. Counting Number of Elements in an Array Using Expansion in Bash

Array expansion is necessary to count the total number of elements in an array. Place a # to count the length of an array or element number before expanding the array using the [@] syntax. Follow the bash script for counting array elements:

#!/bin/bash

arr=("jpg" "png" "doc" "sh" "txt")
count="${#arr[@]}"
echo "The number of elements in the array is: $count"
EXPLANATION

This Bash script initializes an array called arr. It then uses the ${#arr[@]} syntax to determine the number of elements in the array. Finally, it prints the number of elements in the array using the echo command.

Finding length of the array using array expansion in BashThe arr array defined in the code has five elements “jpg” “png” “doc” “sh” and “txt” respectively. The program counts the number of elements of the arr as 5 and prints it in the terminal.

4. Match Patterns and Replace Elements in Array

Array expansion can be used to find and replace elements within an array. You can iterate through the array to find a match or use the slash (/) symbol after expansion to specify the word to find and replace.

Take a look at the script below for a better understanding of how to match patterns and replace elements in array:

#!/bin/bash

# Define an array
my_array=(10 20 Null 5 Null 25)

# Use array expansion to perform the find and replace operation
my_array=("${my_array[@]/Null/0}")

# Print the updated array
echo "${my_array[@]}"
EXPLANATION

The script defines an array called my_array with integer values, including the string Null. It then uses array expansion to perform a find and replace operation, replacing all occurrences of Null with the integer 0 within the array. Finally, it prints the updated array using the echo command.

Replacing using array expansion in BashThe image shows that the program replaces Null with 0 in the (10 20 Null 5 Null 25) array.

Array Creation Using Array Expansion in Bash

Array expansion is useful to create and visualize an array. In this demonstration, I will use the mapfile command to create an array from the contents of a file. Let’s see the bash script to learn how to create array with bash array expansion:

#!/bin/bash

mapfile -t user < "users.txt"
echo "${user[@]}"
EXPLANATION

The script reads the contents of the file named users.txt into an array called user. The mapfile command with the -t option trims trailing newline characters from each line. This way it creates the user array by taking each line of users.txt as separate elements in the array.

First, see the content of the users.txt file by running cat users.txt:

Contents of a fileNow, running the script with ./createarr.sh command will output the below image of array creation:

Creating array from contents of a file

Conclusion

In conclusion, array expansion is super useful for many useful applications. It is mandatory knowledge for slicing an array, replacing elements of an array and combining multiple arrays. I believe from now on you can perform all the above tasks and many more using array expansion in Bash.

People Also Ask

How to count array in Bash?

To count the number of elements present in the bash array, use this syntax count="${#arr[@]}".

Why @ expanded into multiple words in array?

[@]  syntax expands all the words of an array rather than the elements only. Use quotes while expanding in order to expand the element only.

What is the maximum size of a Bash array?

There is no size limitation of a Bash array. Moreover, there is no limitation on the size of an element within the array.

What is the best way to declare elements with whitespaces in an array?

The easiest way to add an element with whitespace is by specifying the index of the element. In case of an associative array use a new key to add an element with whitespace.


Related Articles


<< Go Back to An Overview of Shell Expansion in Bash | Bash Scripting Tutorial 

4.9/5 - (10 votes)
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment