Get the App
SLTechnology News&Howtos  ›  Servers  › 

Methods of creating and using arrays in Bash scripts

Shulou Source: shulou.com Published: 2022-06-02 04:33:20 10月02日 Update

Xiaobian to share with you in the Bash script to create and use the array method, I believe most people do not know how, so share this article for your reference, I hope you read this article after a great harvest, let us go to understand it!

An array is a data structure based on multiple elements of a key pair. Each array element is accessible by key index number. This article will cover creating arrays in bash scripts, initializing arrays, adding elements, updating elements, and deleting elements in bash scripts.

Define an array in Bash

There are two ways to create new arrays in bash scripts. The first is to use the declare command to define an Array. This command defines an associative array named test_array.

$ declare -a test_array

You can also create arrays by assigning elements.

$ test_array=(apple orange lemon)

Access array elements

Similar to other programming languages, bash array elements can be accessed using index numbers starting with 0 and then starting with 1, 2, 3,…n. This also applies to associative arrays with numeric index numbers.

$ echo ${test_array[0]}apple

Prints all elements of an array using @ or * instead of a specific index number.

$ echo $ {test_array [@]} apple orange lemon

Loop through arrays

You can also access array elements using loops in bash scripts. Loops are useful for traversing all array elements one by one and performing some operation on them.

for i in ${test_array[@]}do echo $idon

Add a new element to an array

You can add any number of elements to an existing array using the (+=) operation. Just add new elements, such as:

$ test_array+=(mango banana)

View array elements after adding new:

$ echo ${test_array[@]}apple orange lemon mango banana

Update array elements

To update an array element, simply assign any new value to an existing array by index. Let's change the current array element at index 2 with grapes.

$ test_array[2]=grapes

View array elements after adding new elements:

$ echo ${test_array[@]}apple orange grapes mango banana

Delete array elements

You can simply delete any array element using the index number. Here is how to remove the element at index 2 from the array in the bash script.

$ unset test_array [2]

View array elements after adding new elements:

$ echo ${test_array[@]}apple orange mango banana That's all about creating and using arrays in Bash scripts. Thanks for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Tags: Array element index script index number creation method loop update build content command article association allocation not much just can pass multiple most Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Huawei MariaDB Apple Redmi