| by Arround The Web | No comments

Bash Declare an Empty Array?

Arrays are the collection of the same type of data elements that are stored in contiguous memory locations. Or we can say that it is the simplest form of a data structure whose elements can be accessed directly using the index number where these are stored. Most of the time, it becomes necessary to store the same kind of data in memory instead of creating multiple variables for each data. We can simply use an array to store data of similar type in a single array. Most of all, every programming language supports arrays the same as Linux does. We can also create an array using various commands and methods in Linux.

Using the bash array, we can store the data in contiguous memory. Bash array stores data in the form of indexing or it can also be said that it is a collection of variables. But in the typical array, we can only store the same type of elements but the bash array allows us to store all types of data in single arrays like storing the strings and numbers.

Declaring Array in Bash

There are two types of bash arrays: an indexed array and the associative array. Indexed arrays are those arrays in which elements are stored and are assigned with numeric values starting from “0” to “N” integers. The second one is an associative array based on key-value pairs. Or we can say that it uses a key to map the values in memory.

Example No. 1:

Before declaring an array, we first need to install the bash on our OS. In our case, it is already installed so we do not need to install it again. Now, by just checking its version, we will move toward our main task which is to create an array in bash. By running the below command, we can check the version of bash that is currently installed in our system.

linux@linux-VirtualBox:~$ bash - - version

When we run the command above it will display the details of the bash version. If your bash is not installed, it will prompt an error message in the terminal. As we have the updated version of the bash, it will not upgrade. The output of the command will be displayed on the terminal as shown below.

GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)

Copyright (C) 2019 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

Now let us create an array for that we will run the command that is enlisted below:

linux@linux-VirtualBox:~$ my_array=()

In this command, we simply use the array name by assigning it an empty parenthesis, which means we are declaring an empty array. After running the above command, it will hand over the terminal to us without displaying any output. In memory, the index to the “my_array” will be assigned which can be next used for storing the value to it. Now, we will check whether our empty array is created or not. For checking the array is created, we will run the command mentioned below.

linux@linux-VirtualBox:~$ printf ${#my_array[*]}

In the command above, we have used a printf statement to display the content that is stored in our created array. As we want to display all of the values that are stored in an array “my_array”, we passed the asterisk “*” value in the brackets which is used to print all values of the array.

0

linux@linux-VirtualBox:~$

Example No. 2:

In this example, we will try another method to declare an array. In this method, we will be using the “declare” keyword along with the flag to reserve memory space. For creating an array, we will simply run the below-displayed command:

linux@linux-VirtualBox:~$ declare –a array_a=()

In the command above, the declared keyword is used for the declaration of a variable when we pass an “-a” variable along with the declaration it tends to create and initialize an array. In this, we created an array named “array_a” to which we have not passed any value yet. By just assigning an empty parenthesis to it, we just run the command, it will not display any output in the terminal. By simply running this command, we can declare an array. It reserves a memory location for future use.

Now, we will check whether the array is created or not. For that, we will run the following command in the terminal to get the output.

linux@linux-VirtualBox:~$ printf ${#array_a[@]}

By using a printf statement to print the array, for that we simply passed the array name along with the # sign which implies the number of values to be printed from a specified array: where “@” indicates that all of the values that are stored in the array should be displayed. After running the command above, we will get the output like the example below. As in our array, we have not stored any values so it displayed the  “0” null value.

0

linux@linux-VirtualBox:~$

Example No. 3:

In this illustration, we will try another method to declare an empty array in which we will simply create an array without assigning it any value. By simply running the command below, we will secure an index in memory.

linux@linux-VirtualBox:~$ declare –a array

We declared an array named “array”. This time we have not assigned anything to the array. After running the command, it will not display any output. Just allow us to run further commands as displayed below.

linux@linux-VirtualBox:~$ echo  ${#array[*]}

In this command, we simply passed the array as an argument. After running this command, we will get the output as shown below.

0

linux@linux-VirtualBox:~$

Example No. 3:

In this illustration, we will try to declare an array and then print the message for whether the array is empty or not. For that, we will write the run the command in the terminal that is enlisted below:

linux@linux-VirtualBox:~$ declare –a arr

Using the above command, we declared an array named “arr”. Now, the memory location for “arr” is reserved. Let us create a condition that will execute the output whether the file is empty or not.

linux@linux-VirtualBox:~$ if ! (( ${#arr[*]} –eq 0))then echo “you created an empty array”; fi

In this, we used the if statement in which we passed the condition that if the array “arr” is equal to 0. Then, it will simply display the message “you created an empty array”.

You created an empty array

Conclusion

We have studied creating an array in bash. We discussed the two types of arrays that can be created in the bash and how we can reserve the memory location for an array. Then, by implementing the multiple methods, we created empty arrays. You can try more examples for a better understanding.

Share Button

Source: linuxhint.com

Leave a Reply