Bash is a popular command-line interface that is commonly used on Linux and Unix-based systems. One of the powerful features of Bash is the ability to work with arrays. Bash arrays provide a way to store multiple values in a single variable, making it easy to access and manipulate those values. In this tutorial, we’ll explore how to work with arrays in Bash, including how to declare, initialize, and manipulate arrays.
Declaring Arrays in Bash
To declare an array in Bash, we use the following syntax:
1 | array_name=(value1 value2 ... valueN) |
Here, array_name is the name of the array, and value1, value2, …, valueN are the values we want to store in the array. For example, to declare an array named my_array with three values, we would use the following command:
1 | my_array=(apple banana orange) |
This creates an array named my_array with the values apple, banana, and orange.
Accessing Array Elements
We can access individual elements of an array by using the element index within square brackets. The first element of an array has an index of 0. For example, to access the first element of the my_array array, we would use the following command:
1 | echo ${my_array[0]} |
This would output apple. Similarly, to access the second element of the my_array array, we would use the following command:
1 | echo ${my_array[1]} |
This would output banana.
Iterating Through an Array
We can iterate through an array using a loop. One common loop construct used for this purpose is the for loop. Here’s an example:
1 2 3 4 | for fruit in "${my_array[@]}" do echo $fruit done |
In this example, we use the for loop to iterate through each element in the my_array array. The “${my_array[@]}” syntax is used to expand the array elements into separate words. The echo command is then used to output each element to the console.
Adding Elements to an Array
To add elements to an array, we can use the following syntax:
1 | array_name+=(new_value) |
For example, to add the value grape to the my_array array, we would use the following command:
1 | my_array+=(grape) |
This adds the value grape to the end of the my_array array.
Updating Elements to an Array
To update the array element, simply assign any new value to the existing array by the index. Let’s change the current array element at index 2 with grapes.
1 | my_array[2]=mango |
View the array elements after adding new:
1 2 3 | echo ${test_array[@]} apple banana mango grapes |
Removing Elements from an Array
To remove elements from an array, we can use the unset command. For example, to remove the second element from the my_array array, we would use the following command:
1 | unset my_array[1] |
This removes the second element from the my_array array, leaving us with an array containing apple, orange, and grape.
Conclusion
In this tutorial, we’ve explored how to work with arrays in Bash, including how to declare, initialize, and manipulate arrays. By understanding how to use arrays in Bash, we can make our Bash scripts more powerful and efficient. With practice, you can use arrays to create complex Bash scripts that can handle large amounts of data and perform complex operations on that data.
6 Comments
I am seeing lots of webpages showing how to operate ARRAYS on Bash with Strings but… how to operate them with NUMBER?
And (worst) how to POPULATE then with these numbers from it being initially EMPTY?
And (once more worst) how to populate them with variables that carry numbers (not strings)?
declare -a array=( $(for i in {1..10}; do echo 0; done) ) # empty array
j=0
for i in ${array[@]}
do
echo $i
done
# numbered array
for i in ${array[@]}
do
array[j]=$j
j=$((j+1))
done
for i in ${array[@]}
do
echo $i
done
Hi, how do I add “green apple” to this array?
Hi, You can use following syntax:
test_array=(“green apple” orange lemon)
Loop through an Array, typo instead of `don` should be `done`
Thanks Mike, Article has been updated correctly.