Home >Operation and Maintenance >Linux Operation and Maintenance >How to create and use arrays in Bash script

How to create and use arrays in Bash script

不言
不言Original
2019-03-16 17:35:564797browse

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 introduce creating arrays in bash scripts, as well as initializing arrays, adding elements, updating elements and deleting elements in bash scripts.

How to create and use arrays in Bash script

Defining an Array in Bash

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

$ declare -a test_array

Arrays can also be created by assigning elements.

$ test_array=(apple orange lemon)

Accessing array elements

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

$ echo ${test_array[0]}
apple

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

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

Looping through arrays

You can also use loops in bash scripts to access array elements. Loops are useful for iterating through all array elements one by one and performing some operations on them.

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

Add new elements to an array

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

$ test_array+=(mango banana)

View the array elements after adding new:

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

Update array elements

To update array elements, just Any new values ​​need to be assigned to existing arrays by index. Let's change the current array element at index 2 using grapes.

$ test_array[2]=grapes

View array elements after adding new elements:

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

Deleting array elements

Any array element can be deleted simply using the index number. The following is to remove the element at index 2 from an array in bash script.

$ unset test_array [2]

View the array elements after adding new elements:

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

This article is over here. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to create and use arrays in Bash script. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn