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

不言
Release: 2019-03-16 17:35:56
Original
4803 people have browsed 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 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
Copy after login

Arrays can also be created by assigning elements.

$ test_array=(apple orange lemon)
Copy after login

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
Copy after login

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

$ echo $ {test_array [@]} 
apple orange lemon
Copy after login

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
Copy after login

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)
Copy after login

View the array elements after adding new:

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

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
Copy after login

View array elements after adding new elements:

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

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]
Copy after login

View the array elements after adding new elements:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template