![]() |
|
How to Use Bash Arrays - Printable Version +- My Board (https://ellohost.com/forum) +-- Forum: Tutoriel EN (https://ellohost.com/forum/forumdisplay.php?fid=8) +--- Forum: Others (https://ellohost.com/forum/forumdisplay.php?fid=20) +--- Thread: How to Use Bash Arrays (/showthread.php?tid=88) |
How to Use Bash Arrays - aaron - 07-22-2023 What Is a Bash Array?A bash array is a data structure designed to store information in an indexed way. In other words, a bash array is a large group of variables. Unlike typical arrays used in other programming languages, bash arrays can store different types of elements. For example, you can use a bash array to store both strings and numbers. There are two types of bash arrays:
Remember that bash does not support multidimensional arrays, so it’s not possible to add an array within an array. How to Declare Array in BashThere are a few ways to declare indexed and associative arrays in bash. It’s worth noting that the bash array size does not need to be declared beforehand because bash arrays have no upper limit on the number of elements they can store. Indexed ArrayWe will start with a simple bash indexed array. For example, we’ll use it to create a list of different means of transportation. The same can be achieved without the declare builtin:Or, make it even simpler by going with: Remember that indexing starts at 0, so the above example will assign the car element of the array to the 0 index. However, there is an option to set an array with indices: An interesting feature of bash arrays is that following index numbers in order is not necessary. For example, you can declare only the first and third elements while leaving the second element of an array empty: Associative ArrayWhile indexed arrays don’t require the declare builtin, it won’t be possible to create an associative bash array without declaring it first:Next, add the values. Keep in mind that the key must be a string: An alternative way would be: How to Add a Variable to a Bash ArrayEasily add bash variables using the += operator. For example, the process for an indexed array would look like this:The indexed array has a new element now. Remember that this method appends to the end of an array. Therefore, the motorcycle element will be added as the last element. For associative arrays, the process is very similar. Except, you need to specify the keys along with all the elements: How to Reference and Print an Array ElementUsers can reference bash array values using the element index or key. To do this, create an indexed array: To reference the first array variable, use the following syntax: Combine it with echo, and you will get the following:The output will show you the first element. In this case, it’s car. The same logic applies when referencing and printing an associative array: The output will be car as well. To print the whole array, use @ as an index. The full script looks like this:![]() You can also print the keys of an array instead. To do this, add an exclamation mark before the array name: ![]() How to Remove Bash Array ElementsDeleting array elements is similar to referencing them. Use an index or a key combined with the unset builtin to delete an array element.Here’s an example of deleting a single element from an indexed array: ![]() A similar logic applies to associative arrays: ![]() To delete an entire array, specify unset with the array name as shown here:![]() Nothing is shown after trying to print the array elements because the unset builtin deleted them.How to Loop Through an ArrayCreating bash loops is a fundamental aspect of learning bash scripting basics. You can use loops with arrays as well. For example, the most common use case is to iterate over each array item: ![]() You can also combine keys with the array elements and print them all together like this: ![]() How to Pass an Array to a FunctionFunctions save a considerable amount of time when scripting. Instead of writing the same code repeatedly, you can call out an already written function. We will combine the codeviously mentioned iteration loop and make a function out of it: Running it on the command line will get you the following result: ![]() |