Introduction
Bash is a widely used command-line interpreter for writing scripts. With bash scripting, you can define variables, perform for loops, work with arrays, and so forth. However, we will concentrate on using arrays in bash. Arrays in bash scripting are extremely useful for storing various data sets and basic data structures.
This article will walk you through what bash arrays are, how they differ from other concepts, and how to perform important actions on them using thorough examples. Let’s dive in!
What Are Bash Array?
An array is a collection of multiple elements of a similar data type. However, this is concerning other programming languages. Arrays are unique in Bash. A Bash array is a collection of elements of different data types. Bash arrays can contain values of various data types: strings, numbers, etc.
For example:
Let’s define an array,
my_array=(1 2 3 4 5...10)
The example above is a collection of elements of a similar data type, Numbers. However, this is an array but not a bash array.
my_array=("one" 2 3 4 "five"...10)
This example, however, contains a collection of elements of different data types. This is a Bash array. There is no limit to how large an array may be and it does not need the elements to be indexed or assigned in a logical order.
Having understood what a bash array is, let’s look into more concepts and how to work with bash arrays.
Bash Array Vs. String
Bash also has good support for string operations. Bash like every other programming language and scripting tool presents strings as a data type just like numbers or integers.
A string is a set of values stored as characters rather than numbers. This set of characters could include numbers, but they are contained in a string as characters, not integers. Bash array, on the other hand, stores a set of values of different data types, integers, strings, words, etc.
Example of a string:
string1="Hello world", "Welcome", "12", "two"
The example above contains a set of characters, which includes words, and numbers but all present as a string. One data type!
Example of bash string:
my_array=("one", 2, 3, 4, "five",...10)
A bash array contains different data types, strings, numbers, etc.
How To Declare An Array In Bash
Bash array is declared based on the types of array. There are two types of bash arrays, the numerically indexed array, and the associative array.
The numerically indexed array is an array in which the values are integers(numbers). To declare a numeric array, you must first use the declare command, then specify the type of array. A lowercase, -a is used to specify a numeric array.
A numeric array can be declared in the following way:
declare -a my_array
In the example above, we declared a numeric array with the name my_array.
The second type of array is the associative array. This type of array has its values represented by strings. Just like the numeric array, an associative array is declared using the declare command, followed by the type of array. An upper case -A is used to specify an associative array.
declare -A my_array
How To Add A Variable To A Bash Array
As stated above, we can add a variable to an array by specifying the array type, using their different keys. Next, initialize the array before adding a new variable.
As shown in the example below:
Declare the array
declare -a new_array=("orange", "pear", "grape", "apple")
Initialize the array
new_array=("Fruits")
Add a new variable
new_array[3]=("Apple")
For indexed arrays, we can also add an element by adding it to the end of the array, using the += operator.
Example:
Declare the array
declare -a new_array=("orange", "pear", "grape", "apple")
Initialize the array
new_array=(Fruits)
Add a new variable
new_array+=(Apple)
How To Reference Array Elements
An element in an array can be referenced by adding the index of the element. For example, to reference an element in the array below:
Declare the array
declare -a new_array=("orange", "pear", "grape", "apple")
####reference element
echo ${new_array[2]}
Output:
grape
Note: An array index begins at 0.
In addition, you can also reference all the elements in an array, by adding * or @ as the index. Run these scripts to execute:
declare the array
declare -a new_array=("orange", "pear", "grape", "apple")
reference all elements
echo "${new_array[@]}"
Output:
orange
pear
grape
apple
How To Remove Bash Array Elements
An element or the entire array can be removed using the unset command. If you want to delete just a single element, you must specify the index of the element you intend to remove.
Run the following script to remove an element in an array:
declare the array
declare -a new_array=("orange", "pear", "grape", "apple")
remove an element
unset new_array[2]
Output:
orange
pear
apple
Removing an entire array is much easier. All you have to do is add the name of the array to the unset command.
remove entire elements in an array
unset my_array
How To Print Bash Array
The echo command is used to print an element or elements in an array. As earlier stated, you can use either the @ or * command to reference all the elements in an array. Appending this to the echo command would print all the elements in an array.
Run these commands:
declare the array
declare -a new_array=("orange", "pear", "grape", "apple")
print all elements
echo "${new_array[@]}"
Output:
orange
pear
grape
apple
However, if you want to print a single element in an array, specify the index of the element.
Declare the array
declare -a new_array=("orange", "pear", "grape", "apple")
print element
echo ${new_array[2]}
Output:
grape
How To Pass An Array To A Function
You can pass an array to a function using the ${array[@]} command. This command passes the entire array. However, if you wish to pass only an element in an array, replace the @ command with the index of the element you wish to pass.
Let’s pass an array in a function.
function array_echo() {
arr=("$@")
for i in "${arr[@]}"; do
echo -n "$i "
done
}
my_array=(1 2 3 4 5 )
array_echo "${my_array[@]}"
Output:
1
2
3
4
5
Conclusion
Bash array is a great tool for storing multiple types of data. So far in this article, we covered the basics of bash arrays, the types of bash arrays, and how to perform key operations on them. Although its syntax is different and may seem complex, with constant usage, you will become familiar with the syntax.
The scope of this article is just the basics: you can dive deep into other automation you can execute using bash arrays and other bash operations.
Happy coding!