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.

Advertisement

Declaring Arrays in Bash

To declare an array in Bash, we use the following syntax:

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:

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:

This would output apple. Similarly, to access the second element of the my_array array, we would use the following command:

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:

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:

For example, to add the value grape to the my_array array, we would use the following command:

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.

View the array elements after adding new:

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:

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.

Share.

6 Comments

  1. 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)?

    • Jeevan Sirkunan on

      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

Leave A Reply


Exit mobile version